Skip to content

Commit 922c7f2

Browse files
committed
Removing connection from the get
1 parent 8671258 commit 922c7f2

File tree

3 files changed

+50
-19
lines changed

3 files changed

+50
-19
lines changed

src/from_substrait.cpp

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@
2727

2828
#include "duckdb/main/relation/table_relation.hpp"
2929

30+
#include "duckdb/main/relation/table_function_relation.hpp"
31+
#include "duckdb/main/relation/view_relation.hpp"
32+
#include "duckdb/main/relation/value_relation.hpp"
33+
#include "duckdb/main/relation.hpp"
34+
#include "duckdb/common/helper.hpp"
35+
#include "duckdb/main/table_description.hpp"
36+
#include "duckdb/catalog/catalog_entry/table_catalog_entry.hpp"
37+
3038
namespace duckdb {
3139
const std::unordered_map<std::string, std::string> SubstraitToDuckDB::function_names_remap = {
3240
{"modulus", "mod"}, {"std_dev", "stddev"}, {"starts_with", "prefix"},
@@ -40,7 +48,7 @@ const case_insensitive_set_t SubstraitToDuckDB::valid_extract_subfields = {
4048
"quarter", "microsecond", "milliseconds", "second", "minute", "hour"};
4149

4250
string SubstraitToDuckDB::RemapFunctionName(const string &function_name) {
43-
// Lets first drop any extension id
51+
// Let's first drop any extension id
4452
string name;
4553
for (auto &c : function_name) {
4654
if (c == ':') {
@@ -67,7 +75,11 @@ string SubstraitToDuckDB::RemoveExtension(const string &function_name) {
6775
return name;
6876
}
6977

70-
SubstraitToDuckDB::SubstraitToDuckDB(Connection &con_p, const string &serialized, bool json) : con(con_p) {
78+
void do_nothing(ClientContext*) {}
79+
80+
SubstraitToDuckDB::SubstraitToDuckDB(ClientContext &context_p, const string &serialized, bool json) {
81+
shared_ptr<ClientContext> c_ptr(&context_p, do_nothing);
82+
context = std::move(c_ptr);
7183
if (!json) {
7284
if (!plan.ParseFromString(serialized)) {
7385
throw std::runtime_error("Was not possible to convert binary into Substrait plan");
@@ -511,16 +523,38 @@ shared_ptr<Relation> SubstraitToDuckDB::TransformAggregateOp(const substrait::Re
511523
return make_shared_ptr<AggregateRelation>(TransformOp(sop.aggregate().input()), std::move(expressions),
512524
std::move(groups));
513525
}
526+
unique_ptr<TableDescription> TableInfo(ClientContext& context, const string &schema_name, const string &table_name) {
527+
unique_ptr<TableDescription> result;
528+
// obtain the table info
529+
auto table = Catalog::GetEntry<TableCatalogEntry>(context, INVALID_CATALOG, schema_name, table_name,
530+
OnEntryNotFound::RETURN_NULL);
531+
if (!table) {
532+
return{};
533+
}
534+
// write the table info to the result
535+
result = make_uniq<TableDescription>();
536+
result->schema = schema_name;
537+
result->table = table_name;
538+
for (auto &column : table->GetColumns().Logical()) {
539+
result->columns.emplace_back(column.Copy());
540+
}
541+
return result;
542+
}
514543

515544
shared_ptr<Relation> SubstraitToDuckDB::TransformReadOp(const substrait::Rel &sop) {
516545
auto &sget = sop.read();
517546
shared_ptr<Relation> scan;
518547
if (sget.has_named_table()) {
548+
auto table_name = sget.named_table().names(0);
519549
// If we can't find a table with that name, let's try a view.
520550
try {
521-
scan = con.Table(sget.named_table().names(0));
551+
auto table_info =TableInfo(*context, DEFAULT_SCHEMA, table_name);
552+
if (!table_info) {
553+
throw CatalogException("Table '%s' does not exist!", table_name);
554+
}
555+
return make_shared_ptr<TableRelation>(context, std::move(table_info));
522556
} catch (...) {
523-
scan = con.View(sget.named_table().names(0));
557+
scan = make_shared_ptr<ViewRelation>(context, DEFAULT_SCHEMA, table_name);
524558
}
525559
} else if (sget.has_local_files()) {
526560
vector<Value> parquet_files;
@@ -541,7 +575,9 @@ shared_ptr<Relation> SubstraitToDuckDB::TransformReadOp(const substrait::Rel &so
541575
}
542576
string name = "parquet_" + StringUtil::GenerateRandomName();
543577
named_parameter_map_t named_parameters({{"binary_as_string", Value::BOOLEAN(false)}});
544-
scan = con.TableFunction("parquet_scan", {Value::LIST(parquet_files)}, named_parameters)->Alias(name);
578+
// auto scan_rel = make_shared_ptr<TableFunctionRelation>(context, "parquet_scan", {Value::LIST(parquet_files)}, named_parameters);
579+
// auto rel = static_cast<Relation*>(scan_rel.get());
580+
// scan = rel->Alias(name);
545581
} else if (sget.has_virtual_table()) {
546582
// We need to handle a virtual table as a LogicalExpressionGet
547583
auto literal_values = sget.virtual_table().values();
@@ -554,7 +590,8 @@ shared_ptr<Relation> SubstraitToDuckDB::TransformReadOp(const substrait::Rel &so
554590
}
555591
expression_rows.emplace_back(expression_row);
556592
}
557-
scan = con.Values(expression_rows);
593+
vector<string> column_names;
594+
scan = make_shared_ptr<ValueRelation>(context, expression_rows, column_names, "values");
558595
} else {
559596
throw NotImplementedException("Unsupported type of read operator for substrait");
560597
}

src/include/from_substrait.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace duckdb {
1010

1111
class SubstraitToDuckDB {
1212
public:
13-
SubstraitToDuckDB(Connection &con_p, const string &serialized, bool json = false);
13+
SubstraitToDuckDB(ClientContext &context_p, const string &serialized, bool json = false);
1414
//! Transforms Substrait Plan to DuckDB Relation
1515
shared_ptr<Relation> TransformPlan();
1616

@@ -48,8 +48,8 @@ class SubstraitToDuckDB {
4848

4949
//! Transform Substrait Sort Order to DuckDB Order
5050
OrderByNode TransformOrder(const substrait::SortField &sordf);
51-
//! DuckDB Connection
52-
Connection &con;
51+
//! DuckDB Client Context
52+
shared_ptr<ClientContext> context;
5353
//! Substrait Plan
5454
substrait::Plan plan;
5555
//! Variable used to register functions

src/substrait_extension.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,6 @@ void CleanupConnection(ClientContext& context) const {
9898

9999
};
100100

101-
102-
103-
104-
105101
static void SetOptions(ToSubstraitFunctionData &function, const ClientConfig &config,
106102
const named_parameter_map_t &named_params) {
107103
bool optimizer_option_set = false;
@@ -148,8 +144,8 @@ static unique_ptr<FunctionData> ToJsonBind(ClientContext &context, TableFunction
148144
return InitToSubstraitFunctionData(context.config, input);
149145
}
150146

151-
shared_ptr<Relation> SubstraitPlanToDuckDBRel(Connection &conn, const string &serialized, bool json = false) {
152-
SubstraitToDuckDB transformer_s2d(conn, serialized, json);
147+
shared_ptr<Relation> SubstraitPlanToDuckDBRel(ClientContext &context, const string &serialized, bool json = false) {
148+
SubstraitToDuckDB transformer_s2d(context, serialized, json);
153149
return transformer_s2d.TransformPlan();
154150
}
155151

@@ -159,7 +155,7 @@ static void VerifySubstraitRoundtrip(unique_ptr<LogicalOperator> &query_plan, Cl
159155
auto con = Connection(*context.db);
160156
auto actual_result = con.Query(data.query);
161157

162-
auto sub_relation = SubstraitPlanToDuckDBRel(con, serialized, is_json);
158+
auto sub_relation = SubstraitPlanToDuckDBRel(context, serialized, is_json);
163159
auto substrait_result = sub_relation->Execute();
164160
substrait_result->names = actual_result->names;
165161
unique_ptr<MaterializedQueryResult> substrait_materialized;
@@ -261,18 +257,16 @@ struct FromSubstraitFunctionData : public TableFunctionData {
261257
FromSubstraitFunctionData() = default;
262258
shared_ptr<Relation> plan;
263259
unique_ptr<QueryResult> res;
264-
unique_ptr<Connection> conn;
265260
};
266261

267262
static unique_ptr<FunctionData> SubstraitBind(ClientContext &context, TableFunctionBindInput &input,
268263
vector<LogicalType> &return_types, vector<string> &names, bool is_json) {
269264
auto result = make_uniq<FromSubstraitFunctionData>();
270-
result->conn = make_uniq<Connection>(*context.db);
271265
if (input.inputs[0].IsNull()) {
272266
throw BinderException("from_substrait cannot be called with a NULL parameter");
273267
}
274268
string serialized = input.inputs[0].GetValueUnsafe<string>();
275-
result->plan = SubstraitPlanToDuckDBRel(*result->conn, serialized, is_json);
269+
result->plan = SubstraitPlanToDuckDBRel(context, serialized, is_json);
276270
for (auto &column : result->plan->Columns()) {
277271
return_types.emplace_back(column.Type());
278272
names.emplace_back(column.Name());

0 commit comments

Comments
 (0)