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
28
- #include " duckdb/main/relation/table_relation.hpp"
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"
19
+ #include " substrait_relations.hpp"
34
20
#include " duckdb/common/helper.hpp"
35
21
#include " duckdb/main/table_description.hpp"
36
22
#include " duckdb/catalog/catalog_entry/table_catalog_entry.hpp"
@@ -75,11 +61,8 @@ string SubstraitToDuckDB::RemoveExtension(const string &function_name) {
75
61
return name;
76
62
}
77
63
78
- void do_nothing (ClientContext*) {}
79
64
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);
65
+ SubstraitToDuckDB::SubstraitToDuckDB (shared_ptr<ClientContext> &context_p, const string &serialized, bool json):context(context_p) {
83
66
if (!json) {
84
67
if (!plan.ParseFromString (serialized)) {
85
68
throw std::runtime_error (" Was not possible to convert binary into Substrait plan" );
@@ -454,28 +437,28 @@ shared_ptr<Relation> SubstraitToDuckDB::TransformJoinOp(const substrait::Rel &so
454
437
throw InternalException (" Unsupported join type" );
455
438
}
456
439
unique_ptr<ParsedExpression> join_condition = TransformExpr (sjoin.expression ());
457
- return make_shared_ptr<JoinRelation >(TransformOp (sjoin.left ())->Alias (" left" ),
440
+ return make_shared_ptr<SubstraitJoinRelation >(TransformOp (sjoin.left ())->Alias (" left" ),
458
441
TransformOp (sjoin.right ())->Alias (" right" ), std::move (join_condition),
459
442
djointype);
460
443
}
461
444
462
445
shared_ptr<Relation> SubstraitToDuckDB::TransformCrossProductOp (const substrait::Rel &sop) {
463
446
auto &sub_cross = sop.cross ();
464
447
465
- return make_shared_ptr<CrossProductRelation >(TransformOp (sub_cross.left ())->Alias (" left" ),
448
+ return make_shared_ptr<SubstraitCrossProductRelation >(TransformOp (sub_cross.left ())->Alias (" left" ),
466
449
TransformOp (sub_cross.right ())->Alias (" right" ));
467
450
}
468
451
469
452
shared_ptr<Relation> SubstraitToDuckDB::TransformFetchOp (const substrait::Rel &sop) {
470
453
auto &slimit = sop.fetch ();
471
454
idx_t limit = slimit.count () == -1 ? NumericLimits<idx_t >::Maximum () : slimit.count ();
472
455
idx_t offset = slimit.offset ();
473
- return make_shared_ptr<LimitRelation >(TransformOp (slimit.input ()), limit, offset);
456
+ return make_shared_ptr<SubstraitLimitRelation >(TransformOp (slimit.input ()), limit, offset);
474
457
}
475
458
476
459
shared_ptr<Relation> SubstraitToDuckDB::TransformFilterOp (const substrait::Rel &sop) {
477
460
auto &sfilter = sop.filter ();
478
- return make_shared_ptr<FilterRelation >(TransformOp (sfilter.input ()), TransformExpr (sfilter.condition ()));
461
+ return make_shared_ptr<SubstraitFilterRelation >(TransformOp (sfilter.input ()), TransformExpr (sfilter.condition ()));
479
462
}
480
463
481
464
shared_ptr<Relation> SubstraitToDuckDB::TransformProjectOp (const substrait::Rel &sop) {
@@ -488,7 +471,7 @@ shared_ptr<Relation> SubstraitToDuckDB::TransformProjectOp(const substrait::Rel
488
471
for (size_t i = 0 ; i < expressions.size (); i++) {
489
472
mock_aliases.push_back (" expr_" + to_string (i));
490
473
}
491
- return make_shared_ptr<ProjectionRelation >(TransformOp (sop.project ().input ()), std::move (expressions),
474
+ return make_shared_ptr<SubstraitProjectionRelation >(TransformOp (sop.project ().input ()), std::move (expressions),
492
475
std::move (mock_aliases));
493
476
}
494
477
@@ -520,7 +503,7 @@ shared_ptr<Relation> SubstraitToDuckDB::TransformAggregateOp(const substrait::Re
520
503
nullptr , nullptr , is_distinct));
521
504
}
522
505
523
- return make_shared_ptr<AggregateRelation >(TransformOp (sop.aggregate ().input ()), std::move (expressions),
506
+ return make_shared_ptr<SubstraitAggregateRelation >(TransformOp (sop.aggregate ().input ()), std::move (expressions),
524
507
std::move (groups));
525
508
}
526
509
unique_ptr<TableDescription> TableInfo (ClientContext& context, const string &schema_name, const string &table_name) {
@@ -552,9 +535,9 @@ shared_ptr<Relation> SubstraitToDuckDB::TransformReadOp(const substrait::Rel &so
552
535
if (!table_info) {
553
536
throw CatalogException (" Table '%s' does not exist!" , table_name);
554
537
}
555
- return make_shared_ptr<TableRelation >(context, std::move (table_info));
538
+ return make_shared_ptr<SubstraitTableRelation >(context, std::move (table_info));
556
539
} catch (...) {
557
- scan = make_shared_ptr<ViewRelation >(context, DEFAULT_SCHEMA, table_name);
540
+ scan = make_shared_ptr<SubstraitViewRelation >(context, DEFAULT_SCHEMA, table_name);
558
541
}
559
542
} else if (sget.has_local_files ()) {
560
543
vector<Value> parquet_files;
@@ -575,7 +558,7 @@ shared_ptr<Relation> SubstraitToDuckDB::TransformReadOp(const substrait::Rel &so
575
558
}
576
559
string name = " parquet_" + StringUtil::GenerateRandomName ();
577
560
named_parameter_map_t named_parameters ({{" binary_as_string" , Value::BOOLEAN (false )}});
578
- // auto scan_rel = make_shared_ptr<TableFunctionRelation >(context, "parquet_scan", {Value::LIST(parquet_files)}, named_parameters);
561
+ // auto scan_rel = make_shared_ptr<SubstraitTableFunctionRelation >(context, "parquet_scan", {Value::LIST(parquet_files)}, named_parameters);
579
562
// auto rel = static_cast<Relation*>(scan_rel.get());
580
563
// scan = rel->Alias(name);
581
564
} else if (sget.has_virtual_table ()) {
@@ -591,13 +574,13 @@ shared_ptr<Relation> SubstraitToDuckDB::TransformReadOp(const substrait::Rel &so
591
574
expression_rows.emplace_back (expression_row);
592
575
}
593
576
vector<string> column_names;
594
- scan = make_shared_ptr<ValueRelation >(context, expression_rows, column_names, " values" );
577
+ scan = make_shared_ptr<SubstraitValueRelation >(context, expression_rows, column_names, " values" );
595
578
} else {
596
579
throw NotImplementedException (" Unsupported type of read operator for substrait" );
597
580
}
598
581
599
582
if (sget.has_filter ()) {
600
- scan = make_shared_ptr<FilterRelation >(std::move (scan), TransformExpr (sget.filter ()));
583
+ scan = make_shared_ptr<SubstraitFilterRelation >(std::move (scan), TransformExpr (sget.filter ()));
601
584
}
602
585
603
586
if (sget.has_projection ()) {
@@ -610,7 +593,7 @@ shared_ptr<Relation> SubstraitToDuckDB::TransformReadOp(const substrait::Rel &so
610
593
// TODO make sure nothing else is in there
611
594
expressions.push_back (make_uniq<PositionalReferenceExpression>(sproj.field () + 1 ));
612
595
}
613
- scan = make_shared_ptr<ProjectionRelation >(std::move (scan), std::move (expressions), std::move (aliases));
596
+ scan = make_shared_ptr<SubstraitProjectionRelation >(std::move (scan), std::move (expressions), std::move (aliases));
614
597
}
615
598
616
599
return scan;
@@ -621,7 +604,7 @@ shared_ptr<Relation> SubstraitToDuckDB::TransformSortOp(const substrait::Rel &so
621
604
for (auto &sordf : sop.sort ().sorts ()) {
622
605
order_nodes.push_back (TransformOrder (sordf));
623
606
}
624
- return make_shared_ptr<OrderRelation >(TransformOp (sop.sort ().input ()), std::move (order_nodes));
607
+ return make_shared_ptr<SubstraitOrderRelation >(TransformOp (sop.sort ().input ()), std::move (order_nodes));
625
608
}
626
609
627
610
static SetOperationType TransformSetOperationType (substrait::SetRel_SetOp setop) {
@@ -655,7 +638,7 @@ shared_ptr<Relation> SubstraitToDuckDB::TransformSetOp(const substrait::Rel &sop
655
638
auto lhs = TransformOp (inputs[0 ]);
656
639
auto rhs = TransformOp (inputs[1 ]);
657
640
658
- return make_shared_ptr<SetOpRelation >(std::move (lhs), std::move (rhs), type);
641
+ return make_shared_ptr<SubstraitSetOpRelation >(std::move (lhs), std::move (rhs), type);
659
642
}
660
643
661
644
shared_ptr<Relation> SubstraitToDuckDB::TransformOp (const substrait::Rel &sop) {
@@ -704,11 +687,11 @@ Relation *GetProjection(Relation &relation) {
704
687
case RelationType::PROJECTION_RELATION:
705
688
return &relation;
706
689
case RelationType::LIMIT_RELATION:
707
- return GetProjection (*relation.Cast <LimitRelation >().child );
690
+ return GetProjection (*relation.Cast <SubstraitLimitRelation >().child );
708
691
case RelationType::ORDER_RELATION:
709
- return GetProjection (*relation.Cast <OrderRelation >().child );
692
+ return GetProjection (*relation.Cast <SubstraitOrderRelation >().child );
710
693
case RelationType::SET_OPERATION_RELATION:
711
- return GetProjection (*relation.Cast <SetOpRelation >().right );
694
+ return GetProjection (*relation.Cast <SubstraitSetOpRelation >().right );
712
695
default :
713
696
return nullptr ;
714
697
}
@@ -722,7 +705,7 @@ shared_ptr<Relation> SubstraitToDuckDB::TransformRootOp(const substrait::RelRoot
722
705
auto child = TransformOp (sop.input ());
723
706
auto first_projection_or_table = GetProjection (*child);
724
707
if (first_projection_or_table) {
725
- vector<ColumnDefinition> *column_definitions = &first_projection_or_table->Cast <ProjectionRelation >().columns ;
708
+ vector<ColumnDefinition> *column_definitions = &first_projection_or_table->Cast <SubstraitProjectionRelation >().columns ;
726
709
int32_t i = 0 ;
727
710
for (auto &column : *column_definitions) {
728
711
aliases.push_back (column_names[i++]);
@@ -737,7 +720,7 @@ shared_ptr<Relation> SubstraitToDuckDB::TransformRootOp(const substrait::RelRoot
737
720
}
738
721
}
739
722
740
- return make_shared_ptr<ProjectionRelation >(child, std::move (expressions), aliases);
723
+ return make_shared_ptr<SubstraitProjectionRelation >(child, std::move (expressions), aliases);
741
724
}
742
725
743
726
shared_ptr<Relation> SubstraitToDuckDB::TransformPlan () {
0 commit comments