Skip to content

Commit 3fa6609

Browse files
committed
Final adjustments
1 parent 61bfd1f commit 3fa6609

File tree

4 files changed

+22
-13
lines changed

4 files changed

+22
-13
lines changed

src/from_substrait.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,9 @@ string SubstraitToDuckDB::RemoveExtension(const string &function_name) {
7575
return name;
7676
}
7777

78-
SubstraitToDuckDB::SubstraitToDuckDB(shared_ptr<ClientContext> &context_p, const string &serialized, bool json)
79-
: context(context_p) {
78+
SubstraitToDuckDB::SubstraitToDuckDB(shared_ptr<ClientContext> &context_p, const string &serialized, bool json,
79+
bool acquire_lock_p)
80+
: context(context_p), acquire_lock(acquire_lock_p) {
8081
if (!json) {
8182
if (!plan.ParseFromString(serialized)) {
8283
throw std::runtime_error("Was not possible to convert binary into Substrait plan");
@@ -549,9 +550,9 @@ shared_ptr<Relation> SubstraitToDuckDB::TransformReadOp(const substrait::Rel &so
549550
if (!table_info) {
550551
throw CatalogException("Table '%s' does not exist!", table_name);
551552
}
552-
return make_shared_ptr<TableRelation>(context, std::move(table_info), false);
553+
scan = make_shared_ptr<TableRelation>(context, std::move(table_info), acquire_lock);
553554
} catch (...) {
554-
scan = make_shared_ptr<ViewRelation>(context, DEFAULT_SCHEMA, table_name, false);
555+
scan = make_shared_ptr<ViewRelation>(context, DEFAULT_SCHEMA, table_name, acquire_lock);
555556
}
556557
} else if (sget.has_local_files()) {
557558
vector<Value> parquet_files;
@@ -573,8 +574,8 @@ shared_ptr<Relation> SubstraitToDuckDB::TransformReadOp(const substrait::Rel &so
573574
string name = "parquet_" + StringUtil::GenerateRandomName();
574575
named_parameter_map_t named_parameters({{"binary_as_string", Value::BOOLEAN(false)}});
575576
vector<Value> parameters {Value::LIST(parquet_files)};
576-
auto scan_rel = make_shared_ptr<TableFunctionRelation>(context, "parquet_scan", parameters,
577-
std::move(named_parameters), nullptr, true, false);
577+
auto scan_rel = make_shared_ptr<TableFunctionRelation>(
578+
context, "parquet_scan", parameters, std::move(named_parameters), nullptr, true, acquire_lock);
578579
auto rel = static_cast<Relation *>(scan_rel.get());
579580
scan = rel->Alias(name);
580581
} else if (sget.has_virtual_table()) {
@@ -590,7 +591,7 @@ shared_ptr<Relation> SubstraitToDuckDB::TransformReadOp(const substrait::Rel &so
590591
expression_rows.emplace_back(expression_row);
591592
}
592593
vector<string> column_names;
593-
scan = make_shared_ptr<ValueRelation>(context, expression_rows, column_names, "values", false);
594+
scan = make_shared_ptr<ValueRelation>(context, expression_rows, column_names, "values", acquire_lock);
594595
} else {
595596
throw NotImplementedException("Unsupported type of read operator for substrait");
596597
}

src/include/from_substrait.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ namespace duckdb {
1818

1919
class SubstraitToDuckDB {
2020
public:
21-
SubstraitToDuckDB(shared_ptr<ClientContext> &context_p, const string &serialized, bool json = false);
21+
SubstraitToDuckDB(shared_ptr<ClientContext> &context_p, const string &serialized, bool json = false,
22+
bool acquire_lock = false);
2223
//! Transforms Substrait Plan to DuckDB Relation
2324
shared_ptr<Relation> TransformPlan();
2425

@@ -67,5 +68,7 @@ class SubstraitToDuckDB {
6768
static const unordered_map<std::string, std::string> function_names_remap;
6869
static const case_insensitive_set_t valid_extract_subfields;
6970
vector<ParsedExpression *> struct_expressions;
71+
//! If we should acquire a client context lock when creating the relatiosn
72+
const bool acquire_lock;
7073
};
7174
} // namespace duckdb

src/substrait_extension.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,18 +146,23 @@ static unique_ptr<FunctionData> ToJsonBind(ClientContext &context, TableFunction
146146
}
147147

148148
shared_ptr<Relation> SubstraitPlanToDuckDBRel(shared_ptr<ClientContext> &context, const string &serialized,
149-
bool json = false) {
150-
SubstraitToDuckDB transformer_s2d(context, serialized, json);
149+
bool json = false, bool acquire_lock = false) {
150+
SubstraitToDuckDB transformer_s2d(context, serialized, json, acquire_lock);
151151
return transformer_s2d.TransformPlan();
152152
}
153153

154+
//! This function matches results of substrait plans with direct Duckdb queries
155+
//! Is only executed when pragma enable_verification = true
156+
//! It creates extra connections to be able to execute the consumed DuckDB Plan
157+
//! And the SQL query itself, ideally this wouldn't be necessary and won't
158+
//! work for round-tripping tests over temporary objects.
154159
static void VerifySubstraitRoundtrip(unique_ptr<LogicalOperator> &query_plan, ClientContext &context,
155160
ToSubstraitFunctionData &data, const string &serialized, bool is_json) {
156161
// We round-trip the generated json and verify if the result is the same
157162
auto con = Connection(*context.db);
158163
auto actual_result = con.Query(data.query);
159-
shared_ptr<ClientContext> c_ptr(&context, do_nothing);
160-
auto sub_relation = SubstraitPlanToDuckDBRel(c_ptr, serialized, is_json);
164+
auto con_2 = Connection(*context.db);
165+
auto sub_relation = SubstraitPlanToDuckDBRel(con_2.context, serialized, is_json, true);
161166
auto substrait_result = sub_relation->Execute();
162167
substrait_result->names = actual_result->names;
163168
unique_ptr<MaterializedQueryResult> substrait_materialized;

test/python/test_pyarrow.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,4 @@ def test_substrait_pyarrow(require):
4242

4343
arrow_result = execute_query(connection, "arrow_integers")
4444

45-
assert connection.execute("FROM arrow_result").fetchall() == 0
45+
assert connection.execute("FROM arrow_result").fetchall() == [(0, 'a'), (1, 'b')]

0 commit comments

Comments
 (0)