Skip to content

Commit 636d500

Browse files
Remove internal duckdb::*_ptr usage (#509)
Step 1 in a larger clean up: we should not use duckdb's internal `duckdb::*_ptr`
2 parents 56c26cc + bb06fdd commit 636d500

25 files changed

Lines changed: 796 additions & 770 deletions

src/duckdb_py/duckdb_python.cpp

Lines changed: 90 additions & 86 deletions
Large diffs are not rendered by default.

src/duckdb_py/include/duckdb_python/expression/pyexpression.hpp

Lines changed: 62 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@
2323

2424
namespace duckdb {
2525

26-
struct DuckDBPyExpression : public enable_shared_from_this<DuckDBPyExpression> {
26+
struct DuckDBPyExpression : public std::enable_shared_from_this<DuckDBPyExpression> {
2727
public:
2828
explicit DuckDBPyExpression(unique_ptr<ParsedExpression> expr, OrderType order_type = OrderType::ORDER_DEFAULT,
2929
OrderByNullType null_order = OrderByNullType::ORDER_DEFAULT);
3030

3131
public:
32-
shared_ptr<DuckDBPyExpression> shared_from_this() {
33-
return enable_shared_from_this<DuckDBPyExpression>::shared_from_this();
32+
std::shared_ptr<DuckDBPyExpression> shared_from_this() {
33+
return std::enable_shared_from_this<DuckDBPyExpression>::shared_from_this();
3434
}
3535

3636
public:
@@ -41,92 +41,93 @@ struct DuckDBPyExpression : public enable_shared_from_this<DuckDBPyExpression> {
4141
string ToString() const;
4242
string GetName() const;
4343
void Print() const;
44-
shared_ptr<DuckDBPyExpression> Add(const DuckDBPyExpression &other) const;
45-
shared_ptr<DuckDBPyExpression> Subtract(const DuckDBPyExpression &other) const;
46-
shared_ptr<DuckDBPyExpression> Multiply(const DuckDBPyExpression &other) const;
47-
shared_ptr<DuckDBPyExpression> Division(const DuckDBPyExpression &other) const;
48-
shared_ptr<DuckDBPyExpression> FloorDivision(const DuckDBPyExpression &other) const;
49-
shared_ptr<DuckDBPyExpression> Modulo(const DuckDBPyExpression &other) const;
50-
shared_ptr<DuckDBPyExpression> Power(const DuckDBPyExpression &other) const;
51-
shared_ptr<DuckDBPyExpression> Negate();
44+
std::shared_ptr<DuckDBPyExpression> Add(const DuckDBPyExpression &other) const;
45+
std::shared_ptr<DuckDBPyExpression> Subtract(const DuckDBPyExpression &other) const;
46+
std::shared_ptr<DuckDBPyExpression> Multiply(const DuckDBPyExpression &other) const;
47+
std::shared_ptr<DuckDBPyExpression> Division(const DuckDBPyExpression &other) const;
48+
std::shared_ptr<DuckDBPyExpression> FloorDivision(const DuckDBPyExpression &other) const;
49+
std::shared_ptr<DuckDBPyExpression> Modulo(const DuckDBPyExpression &other) const;
50+
std::shared_ptr<DuckDBPyExpression> Power(const DuckDBPyExpression &other) const;
51+
std::shared_ptr<DuckDBPyExpression> Negate();
5252

5353
// Equality operations
5454

55-
shared_ptr<DuckDBPyExpression> Equality(const DuckDBPyExpression &other);
56-
shared_ptr<DuckDBPyExpression> Inequality(const DuckDBPyExpression &other);
57-
shared_ptr<DuckDBPyExpression> GreaterThan(const DuckDBPyExpression &other);
58-
shared_ptr<DuckDBPyExpression> GreaterThanOrEqual(const DuckDBPyExpression &other);
59-
shared_ptr<DuckDBPyExpression> LessThan(const DuckDBPyExpression &other);
60-
shared_ptr<DuckDBPyExpression> LessThanOrEqual(const DuckDBPyExpression &other);
55+
std::shared_ptr<DuckDBPyExpression> Equality(const DuckDBPyExpression &other);
56+
std::shared_ptr<DuckDBPyExpression> Inequality(const DuckDBPyExpression &other);
57+
std::shared_ptr<DuckDBPyExpression> GreaterThan(const DuckDBPyExpression &other);
58+
std::shared_ptr<DuckDBPyExpression> GreaterThanOrEqual(const DuckDBPyExpression &other);
59+
std::shared_ptr<DuckDBPyExpression> LessThan(const DuckDBPyExpression &other);
60+
std::shared_ptr<DuckDBPyExpression> LessThanOrEqual(const DuckDBPyExpression &other);
6161

62-
shared_ptr<DuckDBPyExpression> SetAlias(const string &alias) const;
63-
shared_ptr<DuckDBPyExpression> When(const DuckDBPyExpression &condition, const DuckDBPyExpression &value);
64-
shared_ptr<DuckDBPyExpression> Else(const DuckDBPyExpression &value);
62+
std::shared_ptr<DuckDBPyExpression> SetAlias(const string &alias) const;
63+
std::shared_ptr<DuckDBPyExpression> When(const DuckDBPyExpression &condition, const DuckDBPyExpression &value);
64+
std::shared_ptr<DuckDBPyExpression> Else(const DuckDBPyExpression &value);
6565

66-
shared_ptr<DuckDBPyExpression> Cast(const DuckDBPyType &type) const;
67-
shared_ptr<DuckDBPyExpression> Between(const DuckDBPyExpression &lower, const DuckDBPyExpression &upper);
68-
shared_ptr<DuckDBPyExpression> Collate(const string &collation);
66+
std::shared_ptr<DuckDBPyExpression> Cast(const DuckDBPyType &type) const;
67+
std::shared_ptr<DuckDBPyExpression> Between(const DuckDBPyExpression &lower, const DuckDBPyExpression &upper);
68+
std::shared_ptr<DuckDBPyExpression> Collate(const string &collation);
6969

7070
// AND, OR and NOT
7171

72-
shared_ptr<DuckDBPyExpression> Not();
73-
shared_ptr<DuckDBPyExpression> And(const DuckDBPyExpression &other) const;
74-
shared_ptr<DuckDBPyExpression> Or(const DuckDBPyExpression &other) const;
72+
std::shared_ptr<DuckDBPyExpression> Not();
73+
std::shared_ptr<DuckDBPyExpression> And(const DuckDBPyExpression &other) const;
74+
std::shared_ptr<DuckDBPyExpression> Or(const DuckDBPyExpression &other) const;
7575

7676
// IS NULL / IS NOT NULL
7777

78-
shared_ptr<DuckDBPyExpression> IsNull();
79-
shared_ptr<DuckDBPyExpression> IsNotNull();
78+
std::shared_ptr<DuckDBPyExpression> IsNull();
79+
std::shared_ptr<DuckDBPyExpression> IsNotNull();
8080

8181
// IN / NOT IN
8282

83-
shared_ptr<DuckDBPyExpression> CreateCompareExpression(ExpressionType compare_type, const py::args &args);
84-
shared_ptr<DuckDBPyExpression> In(const py::args &args);
85-
shared_ptr<DuckDBPyExpression> NotIn(const py::args &args);
83+
std::shared_ptr<DuckDBPyExpression> CreateCompareExpression(ExpressionType compare_type, const py::args &args);
84+
std::shared_ptr<DuckDBPyExpression> In(const py::args &args);
85+
std::shared_ptr<DuckDBPyExpression> NotIn(const py::args &args);
8686

8787
// Order modifiers
8888

89-
shared_ptr<DuckDBPyExpression> Ascending();
90-
shared_ptr<DuckDBPyExpression> Descending();
89+
std::shared_ptr<DuckDBPyExpression> Ascending();
90+
std::shared_ptr<DuckDBPyExpression> Descending();
9191

9292
// Null order modifiers
9393

94-
shared_ptr<DuckDBPyExpression> NullsFirst();
95-
shared_ptr<DuckDBPyExpression> NullsLast();
94+
std::shared_ptr<DuckDBPyExpression> NullsFirst();
95+
std::shared_ptr<DuckDBPyExpression> NullsLast();
9696

9797
public:
9898
const ParsedExpression &GetExpression() const;
99-
shared_ptr<DuckDBPyExpression> Copy() const;
99+
std::shared_ptr<DuckDBPyExpression> Copy() const;
100100

101101
public:
102-
static shared_ptr<DuckDBPyExpression> StarExpression(py::object exclude = py::none());
103-
static shared_ptr<DuckDBPyExpression> ColumnExpression(const py::args &column_name);
104-
static shared_ptr<DuckDBPyExpression> DefaultExpression();
105-
static shared_ptr<DuckDBPyExpression> ConstantExpression(const py::object &value);
106-
static shared_ptr<DuckDBPyExpression> LambdaExpression(const py::object &lhs, const DuckDBPyExpression &rhs);
107-
static shared_ptr<DuckDBPyExpression> CaseExpression(const DuckDBPyExpression &condition,
108-
const DuckDBPyExpression &value);
109-
static shared_ptr<DuckDBPyExpression> FunctionExpression(const string &function_name, const py::args &args);
110-
static shared_ptr<DuckDBPyExpression> Coalesce(const py::args &args);
111-
static shared_ptr<DuckDBPyExpression> SQLExpression(string sql);
102+
static std::shared_ptr<DuckDBPyExpression> StarExpression(py::object exclude = py::none());
103+
static std::shared_ptr<DuckDBPyExpression> ColumnExpression(const py::args &column_name);
104+
static std::shared_ptr<DuckDBPyExpression> DefaultExpression();
105+
static std::shared_ptr<DuckDBPyExpression> ConstantExpression(const py::object &value);
106+
static std::shared_ptr<DuckDBPyExpression> LambdaExpression(const py::object &lhs, const DuckDBPyExpression &rhs);
107+
static std::shared_ptr<DuckDBPyExpression> CaseExpression(const DuckDBPyExpression &condition,
108+
const DuckDBPyExpression &value);
109+
static std::shared_ptr<DuckDBPyExpression> FunctionExpression(const string &function_name, const py::args &args);
110+
static std::shared_ptr<DuckDBPyExpression> Coalesce(const py::args &args);
111+
static std::shared_ptr<DuckDBPyExpression> SQLExpression(string sql);
112112

113113
public:
114114
// Internal functions (not exposed to Python)
115-
static shared_ptr<DuckDBPyExpression> InternalFunctionExpression(const string &function_name,
116-
vector<unique_ptr<ParsedExpression>> children,
117-
bool is_operator = false);
118-
119-
static shared_ptr<DuckDBPyExpression> InternalUnaryOperator(ExpressionType type, const DuckDBPyExpression &arg);
120-
static shared_ptr<DuckDBPyExpression> InternalConjunction(ExpressionType type, const DuckDBPyExpression &arg,
121-
const DuckDBPyExpression &other);
122-
static shared_ptr<DuckDBPyExpression> InternalConstantExpression(Value value);
123-
static shared_ptr<DuckDBPyExpression> BinaryOperator(const string &function_name, const DuckDBPyExpression &arg_one,
124-
const DuckDBPyExpression &arg_two);
125-
static shared_ptr<DuckDBPyExpression> ComparisonExpression(ExpressionType type, const DuckDBPyExpression &left,
126-
const DuckDBPyExpression &right);
127-
static shared_ptr<DuckDBPyExpression> InternalWhen(unique_ptr<duckdb::CaseExpression> expr,
128-
const DuckDBPyExpression &condition,
129-
const DuckDBPyExpression &value);
115+
static std::shared_ptr<DuckDBPyExpression> InternalFunctionExpression(const string &function_name,
116+
vector<unique_ptr<ParsedExpression>> children,
117+
bool is_operator = false);
118+
119+
static std::shared_ptr<DuckDBPyExpression> InternalUnaryOperator(ExpressionType type,
120+
const DuckDBPyExpression &arg);
121+
static std::shared_ptr<DuckDBPyExpression> InternalConjunction(ExpressionType type, const DuckDBPyExpression &arg,
122+
const DuckDBPyExpression &other);
123+
static std::shared_ptr<DuckDBPyExpression> InternalConstantExpression(Value value);
124+
static std::shared_ptr<DuckDBPyExpression>
125+
BinaryOperator(const string &function_name, const DuckDBPyExpression &arg_one, const DuckDBPyExpression &arg_two);
126+
static std::shared_ptr<DuckDBPyExpression> ComparisonExpression(ExpressionType type, const DuckDBPyExpression &left,
127+
const DuckDBPyExpression &right);
128+
static std::shared_ptr<DuckDBPyExpression> InternalWhen(unique_ptr<duckdb::CaseExpression> expr,
129+
const DuckDBPyExpression &condition,
130+
const DuckDBPyExpression &value);
130131
void AssertCaseExpression() const;
131132

132133
private:

src/duckdb_py/include/duckdb_python/numpy/array_wrapper.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ struct NumpyAppendData {
4141
struct ArrayWrapper {
4242
explicit ArrayWrapper(const LogicalType &type, const ClientProperties &client_properties, bool pandas = false);
4343

44-
unique_ptr<RawArrayWrapper> data;
45-
unique_ptr<RawArrayWrapper> mask;
44+
std::unique_ptr<RawArrayWrapper> data;
45+
std::unique_ptr<RawArrayWrapper> mask;
4646
bool requires_mask;
4747
const ClientProperties client_properties;
4848
bool pandas;

src/duckdb_py/include/duckdb_python/pandas/pandas_bind.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ struct RegisteredArray {
1818

1919
struct PandasColumnBindData {
2020
NumpyType numpy_type;
21-
unique_ptr<PandasColumn> pandas_col;
22-
unique_ptr<RegisteredArray> mask;
21+
std::unique_ptr<PandasColumn> pandas_col;
22+
std::unique_ptr<RegisteredArray> mask;
2323
//! Only for categorical types
2424
string internal_categorical_type;
2525
//! Hold ownership of objects created during scanning

src/duckdb_py/include/duckdb_python/pybind11/conversions/pyconnection_default.hpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,19 @@
44
#include "duckdb/common/helper.hpp"
55

66
using duckdb::DuckDBPyConnection;
7-
using duckdb::shared_ptr;
87

98
namespace py = pybind11;
109

1110
namespace PYBIND11_NAMESPACE {
1211
namespace detail {
1312

1413
template <>
15-
class type_caster<shared_ptr<DuckDBPyConnection>>
16-
: public copyable_holder_caster<DuckDBPyConnection, shared_ptr<DuckDBPyConnection>> {
14+
class type_caster<std::shared_ptr<DuckDBPyConnection>>
15+
: public copyable_holder_caster<DuckDBPyConnection, std::shared_ptr<DuckDBPyConnection>> {
1716
using type = DuckDBPyConnection;
18-
using holder_caster = copyable_holder_caster<DuckDBPyConnection, shared_ptr<DuckDBPyConnection>>;
17+
using holder_caster = copyable_holder_caster<DuckDBPyConnection, std::shared_ptr<DuckDBPyConnection>>;
1918
// This is used to generate documentation on duckdb-web
20-
PYBIND11_TYPE_CASTER(shared_ptr<type>, const_name("duckdb.DuckDBPyConnection"));
19+
PYBIND11_TYPE_CASTER(std::shared_ptr<type>, const_name("duckdb.DuckDBPyConnection"));
2120

2221
bool load(handle src, bool convert) {
2322
if (py::none().is(src)) {
@@ -27,17 +26,19 @@ class type_caster<shared_ptr<DuckDBPyConnection>>
2726
if (!holder_caster::load(src, convert)) {
2827
return false;
2928
}
30-
value = std::move(holder);
29+
// pybind11's std::shared_ptr holder_caster (smart_holder bakein) has no `holder` member like the
30+
// generic template did for duckdb::shared_ptr; extract the loaded pointer via its conversion operator.
31+
value = static_cast<std::shared_ptr<type> &>(static_cast<holder_caster &>(*this));
3132
return true;
3233
}
3334

34-
static handle cast(shared_ptr<type> base, return_value_policy rvp, handle h) {
35+
static handle cast(std::shared_ptr<type> base, return_value_policy rvp, handle h) {
3536
return holder_caster::cast(base, rvp, h);
3637
}
3738
};
3839

3940
template <>
40-
struct is_holder_type<DuckDBPyConnection, shared_ptr<DuckDBPyConnection>> : std::true_type {};
41+
struct is_holder_type<DuckDBPyConnection, std::shared_ptr<DuckDBPyConnection>> : std::true_type {};
4142

4243
} // namespace detail
4344
} // namespace PYBIND11_NAMESPACE

0 commit comments

Comments
 (0)