Skip to content
Merged
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
176 changes: 90 additions & 86 deletions src/duckdb_py/duckdb_python.cpp

Large diffs are not rendered by default.

123 changes: 62 additions & 61 deletions src/duckdb_py/include/duckdb_python/expression/pyexpression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@

namespace duckdb {

struct DuckDBPyExpression : public enable_shared_from_this<DuckDBPyExpression> {
struct DuckDBPyExpression : public std::enable_shared_from_this<DuckDBPyExpression> {
public:
explicit DuckDBPyExpression(unique_ptr<ParsedExpression> expr, OrderType order_type = OrderType::ORDER_DEFAULT,
OrderByNullType null_order = OrderByNullType::ORDER_DEFAULT);

public:
shared_ptr<DuckDBPyExpression> shared_from_this() {
return enable_shared_from_this<DuckDBPyExpression>::shared_from_this();
std::shared_ptr<DuckDBPyExpression> shared_from_this() {
return std::enable_shared_from_this<DuckDBPyExpression>::shared_from_this();
}

public:
Expand All @@ -41,92 +41,93 @@ struct DuckDBPyExpression : public enable_shared_from_this<DuckDBPyExpression> {
string ToString() const;
string GetName() const;
void Print() const;
shared_ptr<DuckDBPyExpression> Add(const DuckDBPyExpression &other) const;
shared_ptr<DuckDBPyExpression> Subtract(const DuckDBPyExpression &other) const;
shared_ptr<DuckDBPyExpression> Multiply(const DuckDBPyExpression &other) const;
shared_ptr<DuckDBPyExpression> Division(const DuckDBPyExpression &other) const;
shared_ptr<DuckDBPyExpression> FloorDivision(const DuckDBPyExpression &other) const;
shared_ptr<DuckDBPyExpression> Modulo(const DuckDBPyExpression &other) const;
shared_ptr<DuckDBPyExpression> Power(const DuckDBPyExpression &other) const;
shared_ptr<DuckDBPyExpression> Negate();
std::shared_ptr<DuckDBPyExpression> Add(const DuckDBPyExpression &other) const;
std::shared_ptr<DuckDBPyExpression> Subtract(const DuckDBPyExpression &other) const;
std::shared_ptr<DuckDBPyExpression> Multiply(const DuckDBPyExpression &other) const;
std::shared_ptr<DuckDBPyExpression> Division(const DuckDBPyExpression &other) const;
std::shared_ptr<DuckDBPyExpression> FloorDivision(const DuckDBPyExpression &other) const;
std::shared_ptr<DuckDBPyExpression> Modulo(const DuckDBPyExpression &other) const;
std::shared_ptr<DuckDBPyExpression> Power(const DuckDBPyExpression &other) const;
std::shared_ptr<DuckDBPyExpression> Negate();

// Equality operations

shared_ptr<DuckDBPyExpression> Equality(const DuckDBPyExpression &other);
shared_ptr<DuckDBPyExpression> Inequality(const DuckDBPyExpression &other);
shared_ptr<DuckDBPyExpression> GreaterThan(const DuckDBPyExpression &other);
shared_ptr<DuckDBPyExpression> GreaterThanOrEqual(const DuckDBPyExpression &other);
shared_ptr<DuckDBPyExpression> LessThan(const DuckDBPyExpression &other);
shared_ptr<DuckDBPyExpression> LessThanOrEqual(const DuckDBPyExpression &other);
std::shared_ptr<DuckDBPyExpression> Equality(const DuckDBPyExpression &other);
std::shared_ptr<DuckDBPyExpression> Inequality(const DuckDBPyExpression &other);
std::shared_ptr<DuckDBPyExpression> GreaterThan(const DuckDBPyExpression &other);
std::shared_ptr<DuckDBPyExpression> GreaterThanOrEqual(const DuckDBPyExpression &other);
std::shared_ptr<DuckDBPyExpression> LessThan(const DuckDBPyExpression &other);
std::shared_ptr<DuckDBPyExpression> LessThanOrEqual(const DuckDBPyExpression &other);

shared_ptr<DuckDBPyExpression> SetAlias(const string &alias) const;
shared_ptr<DuckDBPyExpression> When(const DuckDBPyExpression &condition, const DuckDBPyExpression &value);
shared_ptr<DuckDBPyExpression> Else(const DuckDBPyExpression &value);
std::shared_ptr<DuckDBPyExpression> SetAlias(const string &alias) const;
std::shared_ptr<DuckDBPyExpression> When(const DuckDBPyExpression &condition, const DuckDBPyExpression &value);
std::shared_ptr<DuckDBPyExpression> Else(const DuckDBPyExpression &value);

shared_ptr<DuckDBPyExpression> Cast(const DuckDBPyType &type) const;
shared_ptr<DuckDBPyExpression> Between(const DuckDBPyExpression &lower, const DuckDBPyExpression &upper);
shared_ptr<DuckDBPyExpression> Collate(const string &collation);
std::shared_ptr<DuckDBPyExpression> Cast(const DuckDBPyType &type) const;
std::shared_ptr<DuckDBPyExpression> Between(const DuckDBPyExpression &lower, const DuckDBPyExpression &upper);
std::shared_ptr<DuckDBPyExpression> Collate(const string &collation);

// AND, OR and NOT

shared_ptr<DuckDBPyExpression> Not();
shared_ptr<DuckDBPyExpression> And(const DuckDBPyExpression &other) const;
shared_ptr<DuckDBPyExpression> Or(const DuckDBPyExpression &other) const;
std::shared_ptr<DuckDBPyExpression> Not();
std::shared_ptr<DuckDBPyExpression> And(const DuckDBPyExpression &other) const;
std::shared_ptr<DuckDBPyExpression> Or(const DuckDBPyExpression &other) const;

// IS NULL / IS NOT NULL

shared_ptr<DuckDBPyExpression> IsNull();
shared_ptr<DuckDBPyExpression> IsNotNull();
std::shared_ptr<DuckDBPyExpression> IsNull();
std::shared_ptr<DuckDBPyExpression> IsNotNull();

// IN / NOT IN

shared_ptr<DuckDBPyExpression> CreateCompareExpression(ExpressionType compare_type, const py::args &args);
shared_ptr<DuckDBPyExpression> In(const py::args &args);
shared_ptr<DuckDBPyExpression> NotIn(const py::args &args);
std::shared_ptr<DuckDBPyExpression> CreateCompareExpression(ExpressionType compare_type, const py::args &args);
std::shared_ptr<DuckDBPyExpression> In(const py::args &args);
std::shared_ptr<DuckDBPyExpression> NotIn(const py::args &args);

// Order modifiers

shared_ptr<DuckDBPyExpression> Ascending();
shared_ptr<DuckDBPyExpression> Descending();
std::shared_ptr<DuckDBPyExpression> Ascending();
std::shared_ptr<DuckDBPyExpression> Descending();

// Null order modifiers

shared_ptr<DuckDBPyExpression> NullsFirst();
shared_ptr<DuckDBPyExpression> NullsLast();
std::shared_ptr<DuckDBPyExpression> NullsFirst();
std::shared_ptr<DuckDBPyExpression> NullsLast();

public:
const ParsedExpression &GetExpression() const;
shared_ptr<DuckDBPyExpression> Copy() const;
std::shared_ptr<DuckDBPyExpression> Copy() const;

public:
static shared_ptr<DuckDBPyExpression> StarExpression(py::object exclude = py::none());
static shared_ptr<DuckDBPyExpression> ColumnExpression(const py::args &column_name);
static shared_ptr<DuckDBPyExpression> DefaultExpression();
static shared_ptr<DuckDBPyExpression> ConstantExpression(const py::object &value);
static shared_ptr<DuckDBPyExpression> LambdaExpression(const py::object &lhs, const DuckDBPyExpression &rhs);
static shared_ptr<DuckDBPyExpression> CaseExpression(const DuckDBPyExpression &condition,
const DuckDBPyExpression &value);
static shared_ptr<DuckDBPyExpression> FunctionExpression(const string &function_name, const py::args &args);
static shared_ptr<DuckDBPyExpression> Coalesce(const py::args &args);
static shared_ptr<DuckDBPyExpression> SQLExpression(string sql);
static std::shared_ptr<DuckDBPyExpression> StarExpression(py::object exclude = py::none());
static std::shared_ptr<DuckDBPyExpression> ColumnExpression(const py::args &column_name);
static std::shared_ptr<DuckDBPyExpression> DefaultExpression();
static std::shared_ptr<DuckDBPyExpression> ConstantExpression(const py::object &value);
static std::shared_ptr<DuckDBPyExpression> LambdaExpression(const py::object &lhs, const DuckDBPyExpression &rhs);
static std::shared_ptr<DuckDBPyExpression> CaseExpression(const DuckDBPyExpression &condition,
const DuckDBPyExpression &value);
static std::shared_ptr<DuckDBPyExpression> FunctionExpression(const string &function_name, const py::args &args);
static std::shared_ptr<DuckDBPyExpression> Coalesce(const py::args &args);
static std::shared_ptr<DuckDBPyExpression> SQLExpression(string sql);

public:
// Internal functions (not exposed to Python)
static shared_ptr<DuckDBPyExpression> InternalFunctionExpression(const string &function_name,
vector<unique_ptr<ParsedExpression>> children,
bool is_operator = false);

static shared_ptr<DuckDBPyExpression> InternalUnaryOperator(ExpressionType type, const DuckDBPyExpression &arg);
static shared_ptr<DuckDBPyExpression> InternalConjunction(ExpressionType type, const DuckDBPyExpression &arg,
const DuckDBPyExpression &other);
static shared_ptr<DuckDBPyExpression> InternalConstantExpression(Value value);
static shared_ptr<DuckDBPyExpression> BinaryOperator(const string &function_name, const DuckDBPyExpression &arg_one,
const DuckDBPyExpression &arg_two);
static shared_ptr<DuckDBPyExpression> ComparisonExpression(ExpressionType type, const DuckDBPyExpression &left,
const DuckDBPyExpression &right);
static shared_ptr<DuckDBPyExpression> InternalWhen(unique_ptr<duckdb::CaseExpression> expr,
const DuckDBPyExpression &condition,
const DuckDBPyExpression &value);
static std::shared_ptr<DuckDBPyExpression> InternalFunctionExpression(const string &function_name,
vector<unique_ptr<ParsedExpression>> children,
bool is_operator = false);

static std::shared_ptr<DuckDBPyExpression> InternalUnaryOperator(ExpressionType type,
const DuckDBPyExpression &arg);
static std::shared_ptr<DuckDBPyExpression> InternalConjunction(ExpressionType type, const DuckDBPyExpression &arg,
const DuckDBPyExpression &other);
static std::shared_ptr<DuckDBPyExpression> InternalConstantExpression(Value value);
static std::shared_ptr<DuckDBPyExpression>
BinaryOperator(const string &function_name, const DuckDBPyExpression &arg_one, const DuckDBPyExpression &arg_two);
static std::shared_ptr<DuckDBPyExpression> ComparisonExpression(ExpressionType type, const DuckDBPyExpression &left,
const DuckDBPyExpression &right);
static std::shared_ptr<DuckDBPyExpression> InternalWhen(unique_ptr<duckdb::CaseExpression> expr,
const DuckDBPyExpression &condition,
const DuckDBPyExpression &value);
void AssertCaseExpression() const;

private:
Expand Down
4 changes: 2 additions & 2 deletions src/duckdb_py/include/duckdb_python/numpy/array_wrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ struct NumpyAppendData {
struct ArrayWrapper {
explicit ArrayWrapper(const LogicalType &type, const ClientProperties &client_properties, bool pandas = false);

unique_ptr<RawArrayWrapper> data;
unique_ptr<RawArrayWrapper> mask;
std::unique_ptr<RawArrayWrapper> data;
std::unique_ptr<RawArrayWrapper> mask;
bool requires_mask;
const ClientProperties client_properties;
bool pandas;
Expand Down
4 changes: 2 additions & 2 deletions src/duckdb_py/include/duckdb_python/pandas/pandas_bind.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ struct RegisteredArray {

struct PandasColumnBindData {
NumpyType numpy_type;
unique_ptr<PandasColumn> pandas_col;
unique_ptr<RegisteredArray> mask;
std::unique_ptr<PandasColumn> pandas_col;
std::unique_ptr<RegisteredArray> mask;
//! Only for categorical types
string internal_categorical_type;
//! Hold ownership of objects created during scanning
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@
#include "duckdb/common/helper.hpp"

using duckdb::DuckDBPyConnection;
using duckdb::shared_ptr;

namespace py = pybind11;

namespace PYBIND11_NAMESPACE {
namespace detail {

template <>
class type_caster<shared_ptr<DuckDBPyConnection>>
: public copyable_holder_caster<DuckDBPyConnection, shared_ptr<DuckDBPyConnection>> {
class type_caster<std::shared_ptr<DuckDBPyConnection>>
: public copyable_holder_caster<DuckDBPyConnection, std::shared_ptr<DuckDBPyConnection>> {
using type = DuckDBPyConnection;
using holder_caster = copyable_holder_caster<DuckDBPyConnection, shared_ptr<DuckDBPyConnection>>;
using holder_caster = copyable_holder_caster<DuckDBPyConnection, std::shared_ptr<DuckDBPyConnection>>;
// This is used to generate documentation on duckdb-web
PYBIND11_TYPE_CASTER(shared_ptr<type>, const_name("duckdb.DuckDBPyConnection"));
PYBIND11_TYPE_CASTER(std::shared_ptr<type>, const_name("duckdb.DuckDBPyConnection"));

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

static handle cast(shared_ptr<type> base, return_value_policy rvp, handle h) {
static handle cast(std::shared_ptr<type> base, return_value_policy rvp, handle h) {
return holder_caster::cast(base, rvp, h);
}
};

template <>
struct is_holder_type<DuckDBPyConnection, shared_ptr<DuckDBPyConnection>> : std::true_type {};
struct is_holder_type<DuckDBPyConnection, std::shared_ptr<DuckDBPyConnection>> : std::true_type {};

} // namespace detail
} // namespace PYBIND11_NAMESPACE
Loading
Loading