Skip to content

Commit 8384dc3

Browse files
committed
Cleanup from first round of PR review: Rename from TVF to Table UDF, use a dict[str,duckdb.sqltype] for schema, kwargs for create_table_function, clean up tests
1 parent 262ccfd commit 8384dc3

File tree

16 files changed

+943
-472
lines changed

16 files changed

+943
-472
lines changed

duckdb/func/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
from _duckdb._func import ARROW, DEFAULT, NATIVE, SPECIAL, FunctionNullHandling, PythonUDFType # noqa: D104
1+
from _duckdb._func import ARROW, DEFAULT, NATIVE, SPECIAL, FunctionNullHandling, PythonTableUDFType, PythonUDFType # noqa: D104
22

3-
__all__ = ["ARROW", "DEFAULT", "NATIVE", "SPECIAL", "FunctionNullHandling", "PythonUDFType"]
3+
__all__ = ["ARROW", "DEFAULT", "NATIVE", "SPECIAL", "FunctionNullHandling", "PythonTableUDFType", "PythonUDFType"]

duckdb/functional/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
import warnings
44

5-
from duckdb.func import ARROW, DEFAULT, NATIVE, SPECIAL, FunctionNullHandling, PythonTVFType, PythonUDFType
5+
from duckdb.func import ARROW, DEFAULT, NATIVE, SPECIAL, FunctionNullHandling, PythonTableUDFType, PythonUDFType
66

7-
__all__ = ["ARROW", "DEFAULT", "NATIVE", "SPECIAL", "FunctionNullHandling", "PythonTVFType", "PythonUDFType"]
7+
__all__ = ["ARROW", "DEFAULT", "NATIVE", "SPECIAL", "FunctionNullHandling", "PythonTableUDFType", "PythonUDFType"]
88

99
warnings.warn(
1010
"`duckdb.functional` is deprecated and will be removed in a future version. Please use `duckdb.func` instead.",
1111
DeprecationWarning,
1212
stacklevel=2,
13-
)
13+
)

scripts/connection_methods.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@
134134
},
135135
{
136136
"name": "type",
137-
"type": "Optional[PythonTVFType]",
138-
"default": "PythonTVFType.TUPLES"
137+
"type": "Optional[PythonTableUDFType]",
138+
"default": "PythonTableUDFType.TUPLES"
139139
}
140140
],
141141
"return": "DuckDBPyConnection"
@@ -457,7 +457,6 @@
457457
"fetch_record_batch",
458458
"arrow"
459459
],
460-
461460
"function": "FetchRecordBatchReader",
462461
"docs": "Fetch an Arrow RecordBatchReader following execute()",
463462
"args": [
@@ -1139,4 +1138,4 @@
11391138
],
11401139
"return": "None"
11411140
}
1142-
]
1141+
]

src/duckdb_py/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ add_library(
2828
python_dependency.cpp
2929
python_import_cache.cpp
3030
python_replacement_scan.cpp
31-
python_tvf.cpp
31+
python_table_udf.cpp
3232
python_udf.cpp)
3333

3434
target_link_libraries(python_src PRIVATE _duckdb_dependencies)

src/duckdb_py/functional/functional.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ void DuckDBPyFunctional::Initialize(py::module_ &parent) {
1010
.value("ARROW", duckdb::PythonUDFType::ARROW)
1111
.export_values();
1212

13-
py::enum_<duckdb::PythonTVFType>(m, "PythonTVFType")
14-
.value("TUPLES", duckdb::PythonTVFType::TUPLES)
15-
.value("ARROW_TABLE", duckdb::PythonTVFType::ARROW_TABLE)
13+
py::enum_<duckdb::PythonTableUDFType>(m, "PythonTableUDFType")
14+
.value("TUPLES", duckdb::PythonTableUDFType::TUPLES)
15+
.value("ARROW_TABLE", duckdb::PythonTableUDFType::ARROW_TABLE)
1616
.export_values();
1717

1818
py::enum_<duckdb::FunctionNullHandling>(m, "FunctionNullHandling")

src/duckdb_py/include/duckdb_python/pybind11/conversions/python_tvf_type_enum.hpp renamed to src/duckdb_py/include/duckdb_python/pybind11/conversions/python_table_udf_type_enum.hpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,30 @@ using duckdb::StringUtil;
1010

1111
namespace duckdb {
1212

13-
enum class PythonTVFType : uint8_t { TUPLES, ARROW_TABLE };
13+
enum class PythonTableUDFType : uint8_t { TUPLES, ARROW_TABLE };
1414

1515
} // namespace duckdb
1616

17-
using duckdb::PythonTVFType;
17+
using duckdb::PythonTableUDFType;
1818

1919
namespace py = pybind11;
2020

21-
static PythonTVFType PythonTVFTypeFromString(const string &type) {
21+
static PythonTableUDFType PythonTableUDFTypeFromString(const string &type) {
2222
auto ltype = StringUtil::Lower(type);
2323
if (ltype.empty() || ltype == "tuples") {
24-
return PythonTVFType::TUPLES;
24+
return PythonTableUDFType::TUPLES;
2525
} else if (ltype == "arrow_table") {
26-
return PythonTVFType::ARROW_TABLE;
26+
return PythonTableUDFType::ARROW_TABLE;
2727
} else {
2828
throw InvalidInputException("'%s' is not a recognized type for 'tvf_type'", type);
2929
}
3030
}
3131

32-
static PythonTVFType PythonTVFTypeFromInteger(int64_t value) {
32+
static PythonTableUDFType PythonTableUDFTypeFromInteger(int64_t value) {
3333
if (value == 0) {
34-
return PythonTVFType::TUPLES;
34+
return PythonTableUDFType::TUPLES;
3535
} else if (value == 1) {
36-
return PythonTVFType::ARROW_TABLE;
36+
return PythonTableUDFType::ARROW_TABLE;
3737
} else {
3838
throw InvalidInputException("'%d' is not a recognized type for 'tvf_type'", value);
3939
}
@@ -43,27 +43,27 @@ namespace PYBIND11_NAMESPACE {
4343
namespace detail {
4444

4545
template <>
46-
struct type_caster<PythonTVFType> : public type_caster_base<PythonTVFType> {
47-
using base = type_caster_base<PythonTVFType>;
48-
PythonTVFType tmp;
46+
struct type_caster<PythonTableUDFType> : public type_caster_base<PythonTableUDFType> {
47+
using base = type_caster_base<PythonTableUDFType>;
48+
PythonTableUDFType tmp;
4949

5050
public:
5151
bool load(handle src, bool convert) {
5252
if (base::load(src, convert)) {
5353
return true;
5454
} else if (py::isinstance<py::str>(src)) {
55-
tmp = PythonTVFTypeFromString(py::str(src));
55+
tmp = PythonTableUDFTypeFromString(py::str(src));
5656
value = &tmp;
5757
return true;
5858
} else if (py::isinstance<py::int_>(src)) {
59-
tmp = PythonTVFTypeFromInteger(src.cast<int64_t>());
59+
tmp = PythonTableUDFTypeFromInteger(src.cast<int64_t>());
6060
value = &tmp;
6161
return true;
6262
}
6363
return false;
6464
}
6565

66-
static handle cast(PythonTVFType src, return_value_policy policy, handle parent) {
66+
static handle cast(PythonTableUDFType src, return_value_policy policy, handle parent) {
6767
return base::cast(src, policy, parent);
6868
}
6969
};

src/duckdb_py/include/duckdb_python/pyconnection/pyconnection.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#include "duckdb/function/scalar_function.hpp"
2424
#include "duckdb_python/pybind11/conversions/exception_handling_enum.hpp"
2525
#include "duckdb_python/pybind11/conversions/python_udf_type_enum.hpp"
26-
#include "duckdb_python/pybind11/conversions/python_tvf_type_enum.hpp"
26+
#include "duckdb_python/pybind11/conversions/python_table_udf_type_enum.hpp"
2727
#include "duckdb_python/pybind11/conversions/python_csv_line_terminator_enum.hpp"
2828
#include "duckdb/common/shared_ptr.hpp"
2929

@@ -236,9 +236,9 @@ struct DuckDBPyConnection : public enable_shared_from_this<DuckDBPyConnection> {
236236
bool side_effects = false);
237237

238238
shared_ptr<DuckDBPyConnection> RegisterTableFunction(const string &name, const py::function &function,
239-
const py::object &parameters = py::none(),
240-
const py::object &schema = py::none(),
241-
PythonTVFType type = PythonTVFType::TUPLES);
239+
const py::object &schema,
240+
PythonTableUDFType type = PythonTableUDFType::TUPLES,
241+
const py::object &parameters = py::none());
242242

243243
shared_ptr<DuckDBPyConnection> UnregisterTableFunction(const string &name);
244244

@@ -368,7 +368,7 @@ struct DuckDBPyConnection : public enable_shared_from_this<DuckDBPyConnection> {
368368

369369
duckdb::TableFunction CreateTableFunctionFromCallable(const std::string &name, const py::function &callable,
370370
const py::object &parameters, const py::object &schema,
371-
PythonTVFType type);
371+
PythonTableUDFType type);
372372

373373
void RegisterArrowObject(const py::object &arrow_object, const string &name);
374374
vector<unique_ptr<SQLStatement>> GetStatements(const py::object &query);

src/duckdb_py/pyconnection.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -402,11 +402,9 @@ DuckDBPyConnection::RegisterScalarUDF(const string &name, const py::function &ud
402402
return shared_from_this();
403403
}
404404

405-
shared_ptr<DuckDBPyConnection> DuckDBPyConnection::RegisterTableFunction(const string &name,
406-
const py::function &function,
407-
const py::object &parameters,
408-
const py::object &schema,
409-
PythonTVFType type) {
405+
shared_ptr<DuckDBPyConnection>
406+
DuckDBPyConnection::RegisterTableFunction(const string &name, const py::function &function, const py::object &schema,
407+
PythonTableUDFType type, const py::object &parameters) {
410408

411409
auto &connection = con.GetConnection();
412410
auto &context = *connection.context;
@@ -462,12 +460,12 @@ void DuckDBPyConnection::Initialize(py::handle &m) {
462460
connection_module.def("__del__", &DuckDBPyConnection::Close);
463461

464462
connection_module.def("create_table_function", &DuckDBPyConnection::RegisterTableFunction,
465-
"Register a table valued function via Callable", py::arg("name"), py::arg("callable"),
466-
py::arg("parameters") = py::none(), py::arg("schema") = py::none(),
467-
py::arg("type") = PythonTVFType::TUPLES);
463+
"Register a table user defined function via Callable", py::arg("name"), py::arg("callable"),
464+
py::arg("schema"), py::kw_only(), py::arg("type") = PythonTableUDFType::TUPLES,
465+
py::arg("parameters") = py::none());
468466

469467
connection_module.def("unregister_table_function", &DuckDBPyConnection::UnregisterTableFunction,
470-
"Unregister a table valued function", py::arg("name"));
468+
"Unregister a table user defined function", py::arg("name"));
471469

472470
InitializeConnectionMethods(connection_module);
473471
connection_module.def_property_readonly("description", &DuckDBPyConnection::GetDescription,

0 commit comments

Comments
 (0)