2
2
3
3
#include " duckdb/common/types/value.hpp"
4
4
#include " duckdb/parser/expression/list.hpp"
5
- #include " duckdb/main/relation/join_relation.hpp"
6
- #include " duckdb/main/relation/cross_product_relation.hpp"
7
-
8
- #include " duckdb/main/relation/limit_relation.hpp"
9
- #include " duckdb/main/relation/projection_relation.hpp"
10
- #include " duckdb/main/relation/setop_relation.hpp"
11
- #include " duckdb/main/relation/aggregate_relation.hpp"
12
- #include " duckdb/main/relation/filter_relation.hpp"
13
- #include " duckdb/main/relation/order_relation.hpp"
14
5
#include " duckdb/main/connection.hpp"
15
6
#include " duckdb/parser/parser.hpp"
16
7
#include " duckdb/common/exception.hpp"
25
16
#include " google/protobuf/util/json_util.h"
26
17
#include " substrait/plan.pb.h"
27
18
19
+ #include " duckdb/main/table_description.hpp"
20
+
21
+ #include " duckdb/catalog/catalog_entry/table_catalog_entry.hpp"
22
+ #include " duckdb/common/helper.hpp"
23
+
24
+ #include " duckdb/main/relation.hpp"
28
25
#include " duckdb/main/relation/table_relation.hpp"
26
+ #include " duckdb/main/relation/table_function_relation.hpp"
27
+ #include " duckdb/main/relation/value_relation.hpp"
28
+ #include " duckdb/main/relation/view_relation.hpp"
29
+ #include " duckdb/main/relation/aggregate_relation.hpp"
30
+ #include " duckdb/main/relation/cross_product_relation.hpp"
31
+ #include " duckdb/main/relation/filter_relation.hpp"
32
+ #include " duckdb/main/relation/join_relation.hpp"
33
+ #include " duckdb/main/relation/limit_relation.hpp"
34
+ #include " duckdb/main/relation/order_relation.hpp"
35
+ #include " duckdb/main/relation/projection_relation.hpp"
36
+ #include " duckdb/main/relation/setop_relation.hpp"
29
37
30
38
namespace duckdb {
31
39
const std::unordered_map<std::string, std::string> SubstraitToDuckDB::function_names_remap = {
@@ -40,7 +48,7 @@ const case_insensitive_set_t SubstraitToDuckDB::valid_extract_subfields = {
40
48
" quarter" , " microsecond" , " milliseconds" , " second" , " minute" , " hour" };
41
49
42
50
string SubstraitToDuckDB::RemapFunctionName (const string &function_name) {
43
- // Lets first drop any extension id
51
+ // Let's first drop any extension id
44
52
string name;
45
53
for (auto &c : function_name) {
46
54
if (c == ' :' ) {
@@ -67,7 +75,9 @@ string SubstraitToDuckDB::RemoveExtension(const string &function_name) {
67
75
return name;
68
76
}
69
77
70
- SubstraitToDuckDB::SubstraitToDuckDB (Connection &con_p, const string &serialized, bool json) : con(con_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) {
71
81
if (!json) {
72
82
if (!plan.ParseFromString (serialized)) {
73
83
throw std::runtime_error (" Was not possible to convert binary into Substrait plan" );
@@ -510,16 +520,46 @@ shared_ptr<Relation> SubstraitToDuckDB::TransformAggregateOp(const substrait::Re
510
520
return make_shared_ptr<AggregateRelation>(TransformOp (sop.aggregate ().input ()), std::move (expressions),
511
521
std::move (groups));
512
522
}
523
+ unique_ptr<TableDescription> TableInfo (ClientContext &context, const string &schema_name, const string &table_name) {
524
+ // obtain the table info
525
+ auto table = Catalog::GetEntry<TableCatalogEntry>(context, INVALID_CATALOG, schema_name, table_name,
526
+ OnEntryNotFound::RETURN_NULL);
527
+ if (!table) {
528
+ return {};
529
+ }
530
+ // write the table info to the result
531
+ auto result = make_uniq<TableDescription>(INVALID_CATALOG, schema_name, table_name);
532
+ for (auto &column : table->GetColumns ().Logical ()) {
533
+ result->columns .emplace_back (column.Copy ());
534
+ }
535
+ return result;
536
+ }
513
537
514
538
shared_ptr<Relation> SubstraitToDuckDB::TransformReadOp (const substrait::Rel &sop) {
515
539
auto &sget = sop.read ();
516
540
shared_ptr<Relation> scan;
541
+ auto context_wrapper = make_shared_ptr<RelationContextWrapper>(context);
517
542
if (sget.has_named_table ()) {
543
+ auto table_name = sget.named_table ().names (0 );
518
544
// If we can't find a table with that name, let's try a view.
519
545
try {
520
- scan = con.Table (sget.named_table ().names (0 ));
546
+ auto table_info = TableInfo (*context, DEFAULT_SCHEMA, table_name);
547
+ if (!table_info) {
548
+ throw CatalogException (" Table '%s' does not exist!" , table_name);
549
+ }
550
+ if (acquire_lock) {
551
+ scan = make_shared_ptr<TableRelation>(context, std::move (table_info));
552
+
553
+ } else {
554
+ scan = make_shared_ptr<TableRelation>(context_wrapper, std::move (table_info));
555
+ }
521
556
} catch (...) {
522
- scan = con.View (sget.named_table ().names (0 ));
557
+ if (acquire_lock) {
558
+ scan = make_shared_ptr<ViewRelation>(context, DEFAULT_SCHEMA, table_name);
559
+
560
+ } else {
561
+ scan = make_shared_ptr<ViewRelation>(context_wrapper, DEFAULT_SCHEMA, table_name);
562
+ }
523
563
}
524
564
} else if (sget.has_local_files ()) {
525
565
vector<Value> parquet_files;
@@ -540,7 +580,18 @@ shared_ptr<Relation> SubstraitToDuckDB::TransformReadOp(const substrait::Rel &so
540
580
}
541
581
string name = " parquet_" + StringUtil::GenerateRandomName ();
542
582
named_parameter_map_t named_parameters ({{" binary_as_string" , Value::BOOLEAN (false )}});
543
- scan = con.TableFunction (" parquet_scan" , {Value::LIST (parquet_files)}, named_parameters)->Alias (name);
583
+ vector<Value> parameters {Value::LIST (parquet_files)};
584
+ shared_ptr<TableFunctionRelation> scan_rel;
585
+ if (acquire_lock) {
586
+ scan_rel = make_shared_ptr<TableFunctionRelation>(context, " parquet_scan" , parameters,
587
+ std::move (named_parameters));
588
+ } else {
589
+ scan_rel = make_shared_ptr<TableFunctionRelation>(context_wrapper, " parquet_scan" , parameters,
590
+ std::move (named_parameters));
591
+ }
592
+
593
+ auto rel = static_cast <Relation *>(scan_rel.get ());
594
+ scan = rel->Alias (name);
544
595
} else if (sget.has_virtual_table ()) {
545
596
// We need to handle a virtual table as a LogicalExpressionGet
546
597
auto literal_values = sget.virtual_table ().values ();
@@ -553,7 +604,13 @@ shared_ptr<Relation> SubstraitToDuckDB::TransformReadOp(const substrait::Rel &so
553
604
}
554
605
expression_rows.emplace_back (expression_row);
555
606
}
556
- scan = con.Values (expression_rows);
607
+ vector<string> column_names;
608
+ if (acquire_lock) {
609
+ scan = make_shared_ptr<ValueRelation>(context, expression_rows, column_names);
610
+
611
+ } else {
612
+ scan = make_shared_ptr<ValueRelation>(context_wrapper, expression_rows, column_names);
613
+ }
557
614
} else {
558
615
throw NotImplementedException (" Unsupported type of read operator for substrait" );
559
616
}
0 commit comments