Skip to content
This repository has been archived by the owner on Nov 21, 2024. It is now read-only.

Commit

Permalink
Support for Read from Virtual Table with input as rows of expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
anshuldata committed Oct 30, 2024
1 parent a40c2ab commit 81ac8b4
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 10 deletions.
2 changes: 1 addition & 1 deletion duckdb
37 changes: 28 additions & 9 deletions src/from_substrait.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

#include "duckdb/main/relation/create_table_relation.hpp"
#include <duckdb/main/relation/delete_relation.hpp>
#include <duckdb/main/relation/value_relation.hpp>

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

namespace duckdb {
Expand Down Expand Up @@ -545,17 +547,34 @@ shared_ptr<Relation> SubstraitToDuckDB::TransformReadOp(const substrait::Rel &so
scan = con.TableFunction("parquet_scan", {Value::LIST(parquet_files)}, named_parameters)->Alias(name);
} else if (sget.has_virtual_table()) {
// We need to handle a virtual table as a LogicalExpressionGet
auto literal_values = sget.virtual_table().values();
vector<vector<Value>> expression_rows;
for (auto &row : literal_values) {
auto values = row.fields();
vector<Value> expression_row;
for (const auto &value : values) {
expression_row.emplace_back(TransformLiteralToValue(value));
if (!sget.virtual_table().values().empty()) {

Check warning on line 550 in src/from_substrait.cpp

View workflow job for this annotation

GitHub Actions / Build extension binaries / MacOS (osx_amd64, x86_64, x64-osx)

'values' is deprecated [-Wdeprecated-declarations]

Check warning on line 550 in src/from_substrait.cpp

View workflow job for this annotation

GitHub Actions / Build extension binaries / MacOS (osx_arm64, arm64, arm64-osx)

'values' is deprecated [-Wdeprecated-declarations]
auto literal_values = sget.virtual_table().values();

Check warning on line 551 in src/from_substrait.cpp

View workflow job for this annotation

GitHub Actions / Build extension binaries / MacOS (osx_amd64, x86_64, x64-osx)

'values' is deprecated [-Wdeprecated-declarations]

Check warning on line 551 in src/from_substrait.cpp

View workflow job for this annotation

GitHub Actions / Build extension binaries / MacOS (osx_arm64, arm64, arm64-osx)

'values' is deprecated [-Wdeprecated-declarations]
vector<vector<Value>> expression_rows;
for (auto &row : literal_values) {
auto values = row.fields();
vector<Value> expression_row;
for (const auto &value : values) {
expression_row.emplace_back(TransformLiteralToValue(value));
}
expression_rows.emplace_back(expression_row);
}
expression_rows.emplace_back(expression_row);
scan = con.Values(expression_rows);
} else
{
auto expression_rows = sget.virtual_table().expressions();
vector<vector<unique_ptr<ParsedExpression>>> expressions;
for (auto &row : expression_rows) {
vector<unique_ptr<ParsedExpression>> expression_row;
for (const auto &expr : row.fields()) {
expression_row.emplace_back(TransformExpr(expr));
}
expressions.emplace_back(std::move(expression_row));
}
vector<string> column_names;
auto context = con.context;
scan = make_shared_ptr<ValueRelation>(context, std::move(expressions), column_names);
}
scan = con.Values(expression_rows);

} else {
throw NotImplementedException("Unsupported type of read operator for substrait");
}
Expand Down
1 change: 1 addition & 0 deletions src/include/to_substrait.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class DuckDBToSubstrait {
substrait::Rel *TransformComparisonJoin(LogicalOperator &dop);
substrait::Rel *TransformAggregateGroup(LogicalOperator &dop);
substrait::Rel *TransformGet(LogicalOperator &dop);
substrait::Rel *TransformExpressionGet(LogicalOperator &dop);
substrait::Rel *TransformCrossProduct(LogicalOperator &dop);
substrait::Rel *TransformUnion(LogicalOperator &dop);
substrait::Rel *TransformDistinct(LogicalOperator &dop);
Expand Down
21 changes: 21 additions & 0 deletions src/to_substrait.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1339,6 +1339,25 @@ substrait::Rel *DuckDBToSubstrait::TransformGet(LogicalOperator &dop) {
return get_rel;
}

substrait::Rel *DuckDBToSubstrait::TransformExpressionGet(LogicalOperator &dop) {
auto get_rel = new substrait::Rel();
auto &dget = dop.Cast<LogicalExpressionGet>();

auto sget = get_rel->mutable_read();
auto virtual_table = sget->mutable_virtual_table();

for (auto &row : dget.expressions) {
auto row_item = virtual_table->add_expressions();
for (auto &expr : row) {
auto s_expr = new substrait::Expression();
TransformExpr(*expr, *s_expr);
*row_item->add_fields() = *s_expr;
delete s_expr;
}
}
return get_rel;
}

substrait::Rel *DuckDBToSubstrait::TransformCrossProduct(LogicalOperator &dop) {
auto rel = new substrait::Rel();
auto sub_cross_prod = rel->mutable_cross();
Expand Down Expand Up @@ -1537,6 +1556,8 @@ substrait::Rel *DuckDBToSubstrait::TransformOp(LogicalOperator &dop) {
return TransformAggregateGroup(dop);
case LogicalOperatorType::LOGICAL_GET:
return TransformGet(dop);
case LogicalOperatorType::LOGICAL_EXPRESSION_GET:
return TransformExpressionGet(dop);
case LogicalOperatorType::LOGICAL_CROSS_PRODUCT:
return TransformCrossProduct(dop);
case LogicalOperatorType::LOGICAL_UNION:
Expand Down
24 changes: 24 additions & 0 deletions test/c/test_substrait_c_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,27 @@ TEST_CASE("Test C DeleteRows with Substrait API", "[substrait-api]") {
REQUIRE(CHECK_COLUMN(result, 2, {1, 2, 3}));
REQUIRE(CHECK_COLUMN(result, 3, {120000, 80000, 95000}));
}

TEST_CASE("Test C VirtualTable input Literal", "[substrait-api]") {
DuckDB db(nullptr);
Connection con(db);
//con.EnableQueryVerification();

auto json = con.GetSubstraitJSON("select * from (values (1, 2),(3, 4))");
auto result = con.FromSubstraitJSON(json);

REQUIRE(CHECK_COLUMN(result, 0, {1, 3}));
REQUIRE(CHECK_COLUMN(result, 1, {2, 4}));
}

TEST_CASE("Test C VirtualTable input Expression", "[substrait-api]") {
DuckDB db(nullptr);
Connection con(db);
//con.EnableQueryVerification();

auto json = con.GetSubstraitJSON("select * from (values (1+1,2+2),(3+3,4+4)) as temp(a,b)");
auto result = con.FromSubstraitJSON(json);

REQUIRE(CHECK_COLUMN(result, 0, {2, 6}));
REQUIRE(CHECK_COLUMN(result, 1, {4, 8}));
}

0 comments on commit 81ac8b4

Please sign in to comment.