-
-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add simple StoragePython to fix compile flags
- Loading branch information
Showing
3 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#include "StoragePython.h" | ||
#include "pybind11/embed.h" | ||
|
||
namespace py = pybind11; | ||
|
||
namespace DB | ||
{ | ||
|
||
StoragePython::StoragePython( | ||
const StorageID & table_id_, const String & python_class_name_, const ColumnsDescription & columns_, ContextPtr context_) | ||
: IStorage(table_id_), python_class_name(python_class_name_) | ||
{ | ||
// Initialize the Python interpreter and pybind11 | ||
py::scoped_interpreter guard{}; // Ensure the Python interpreter is initialized only once | ||
|
||
// Load the user's Python class | ||
py::module_ user_module = py::module_::import("user_module_name"); | ||
python_class_instance = user_module.attr(python_class_name.c_str())(); | ||
} | ||
|
||
Pipe StoragePython::read( | ||
const Names & column_names, | ||
const StorageSnapshotPtr & storage_snapshot, | ||
SelectQueryInfo & query_info, | ||
ContextPtr context, | ||
QueryProcessingStage::Enum, | ||
size_t max_block_size, | ||
size_t /*num_streams*/) | ||
{ | ||
// Here, a simple call to the Python `read` method would be made, and its results used. | ||
// Actual implementation would depend on how you wish to handle the data conversion. | ||
py::bytes result = python_class_instance.attr("read")(max_block_size); | ||
// Transform `result` to a ClickHouse `Pipe` object | ||
// This part is left as an exercise for the reader | ||
} | ||
|
||
SinkToStoragePtr StoragePython::write( | ||
const ASTPtr & /* query */, const StorageMetadataPtr & metadata_snapshot, ContextPtr /*context*/, bool /*async_insert*/) | ||
{ | ||
// Similarly, a simple call to the Python `write` method would be made here. | ||
// This example does not include error handling or data transformation for brevity. | ||
// python_class_instance.attr("write")(data_to_write); | ||
|
||
// For demonstration, this does not actually write but shows how you might call the method. | ||
// Actual data writing logic and conversion to suitable types would need to be implemented. | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#pragma once | ||
|
||
#include <memory> | ||
#include <Storages/IStorage.h> | ||
#include "pybind11/pybind11.h" | ||
|
||
namespace py = pybind11; | ||
|
||
namespace DB | ||
{ | ||
|
||
class StoragePython final : public IStorage | ||
{ | ||
public: | ||
StoragePython(const StorageID & table_id_, const String & python_class_name_, const ColumnsDescription & columns_, ContextPtr context_); | ||
|
||
std::string getName() const override { return "Python"; } | ||
|
||
// Override the read method in IStorage | ||
Pipe read( | ||
const Names & column_names, | ||
const StorageSnapshotPtr & storage_snapshot, | ||
SelectQueryInfo & query_info, | ||
ContextPtr context, | ||
QueryProcessingStage::Enum processed_stage, | ||
size_t max_block_size, | ||
size_t num_streams) override; | ||
|
||
// Override the write method in IStorage | ||
SinkToStoragePtr | ||
write(const ASTPtr & query, const StorageMetadataPtr & metadata_snapshot, ContextPtr context, bool async_insert) override; | ||
|
||
private: | ||
String python_class_name; | ||
py::object python_class_instance; // To store the instance of the Python class | ||
}; | ||
|
||
} |