27
27
28
28
#include " duckdb/main/relation/table_relation.hpp"
29
29
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
+
30
38
namespace duckdb {
31
39
const std::unordered_map<std::string, std::string> SubstraitToDuckDB::function_names_remap = {
32
40
{" modulus" , " mod" }, {" std_dev" , " stddev" }, {" starts_with" , " prefix" },
@@ -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,11 @@ 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
+ 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);
71
83
if (!json) {
72
84
if (!plan.ParseFromString (serialized)) {
73
85
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
511
523
return make_shared_ptr<AggregateRelation>(TransformOp (sop.aggregate ().input ()), std::move (expressions),
512
524
std::move (groups));
513
525
}
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
+ }
514
543
515
544
shared_ptr<Relation> SubstraitToDuckDB::TransformReadOp (const substrait::Rel &sop) {
516
545
auto &sget = sop.read ();
517
546
shared_ptr<Relation> scan;
518
547
if (sget.has_named_table ()) {
548
+ auto table_name = sget.named_table ().names (0 );
519
549
// If we can't find a table with that name, let's try a view.
520
550
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));
522
556
} catch (...) {
523
- scan = con. View (sget. named_table (). names ( 0 ) );
557
+ scan = make_shared_ptr<ViewRelation>(context, DEFAULT_SCHEMA, table_name );
524
558
}
525
559
} else if (sget.has_local_files ()) {
526
560
vector<Value> parquet_files;
@@ -541,7 +575,9 @@ shared_ptr<Relation> SubstraitToDuckDB::TransformReadOp(const substrait::Rel &so
541
575
}
542
576
string name = " parquet_" + StringUtil::GenerateRandomName ();
543
577
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);
545
581
} else if (sget.has_virtual_table ()) {
546
582
// We need to handle a virtual table as a LogicalExpressionGet
547
583
auto literal_values = sget.virtual_table ().values ();
@@ -554,7 +590,8 @@ shared_ptr<Relation> SubstraitToDuckDB::TransformReadOp(const substrait::Rel &so
554
590
}
555
591
expression_rows.emplace_back (expression_row);
556
592
}
557
- scan = con.Values (expression_rows);
593
+ vector<string> column_names;
594
+ scan = make_shared_ptr<ValueRelation>(context, expression_rows, column_names, " values" );
558
595
} else {
559
596
throw NotImplementedException (" Unsupported type of read operator for substrait" );
560
597
}
0 commit comments