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

<DRAFT> Insert table as select #3

Closed
wants to merge 9 commits into from
19 changes: 17 additions & 2 deletions src/to_substrait.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1313,6 +1313,22 @@ substrait::Rel *DuckDBToSubstrait::TransformGet(LogicalOperator &dop) {
return get_rel;
}

static substrait::Expression_Literal GetLiteralFromSubstraitExpression(const substrait::Expression &expr) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you make this a class member?

substrait::Expression_Literal DuckDBToSubstrait::ToExpressionLiteral

substrait::Expression_Literal literal_field;
switch (expr.rex_type_case())
{
case substrait::Expression::kLiteral:
literal_field = expr.literal();
break;
case substrait::Expression::kCast:
literal_field = GetLiteralFromSubstraitExpression(expr.cast().input());
break;
default:
throw NotImplementedException("Unimplemented type of expression to fetch literal");
}
return literal_field;
}

substrait::Rel *DuckDBToSubstrait::TransformExpressionGet(LogicalOperator &dop) {
auto get_rel = new substrait::Rel();
auto &dget = dop.Cast<LogicalExpressionGet>();
Expand All @@ -1325,11 +1341,10 @@ substrait::Rel *DuckDBToSubstrait::TransformExpressionGet(LogicalOperator &dop)
for (auto &expr : row) {
auto s_expr = new substrait::Expression();
TransformExpr(*expr, *s_expr);
*row_item->add_fields() = s_expr->literal();
*row_item->add_fields() = GetLiteralFromSubstraitExpression(*s_expr);
delete s_expr;
}
}

return get_rel;
}

Expand Down
64 changes: 62 additions & 2 deletions test/c/test_substrait_c_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ TEST_CASE("Test C Get and To Json-Substrait API", "[substrait-api]") {
REQUIRE_THROWS(con.FromSubstraitJSON("this is not valid"));
}

TEST_CASE("Test C Get and To Substrait API for Insert as select", "[substrait-api]") {
TEST_CASE("Test C Get and To Substrait API for Insert from select", "[substrait-api]") {
DuckDB db(nullptr);
SubstraitExtension substrait_extension;
substrait_extension.Load(db);
Expand All @@ -77,7 +77,7 @@ TEST_CASE("Test C Get and To Substrait API for Insert as select", "[substrait-ap
REQUIRE(CHECK_COLUMN(result, 0, {4}));
}

TEST_CASE("Test C Get and To Json-Substrait API for Insert as select", "[substrait-api]") {
TEST_CASE("Test C Get and To Json-Substrait API for Insert from select", "[substrait-api]") {
DuckDB db(nullptr);
SubstraitExtension substrait_extension;
substrait_extension.Load(db);
Expand All @@ -97,4 +97,64 @@ TEST_CASE("Test C Get and To Json-Substrait API for Insert as select", "[substra

// number of rows inserted are expected as result of insert
REQUIRE(CHECK_COLUMN(result, 0, {4}));
}

TEST_CASE("Test C Get and To Substrait API for Insert from virtual table", "[substrait-api]") {
DuckDB db(nullptr);
SubstraitExtension substrait_extension;
substrait_extension.Load(db);
Connection con(db);
con.EnableQueryVerification();

REQUIRE_NO_FAIL(con.Query("CREATE TABLE t1(i INTEGER)"));

auto proto = con.GetSubstrait("INSERT INTO t1 VALUES (1), (2), (3), (NULL)");
auto result = con.FromSubstrait(proto);
// number of rows inserted are expected as result of insert
REQUIRE(CHECK_COLUMN(result, 0, {4}));
}

TEST_CASE("Test C Get and To JSON-Substrait API for Insert from virtual table", "[substrait-api]") {
DuckDB db(nullptr);
SubstraitExtension substrait_extension;
substrait_extension.Load(db);
Connection con(db);
con.EnableQueryVerification();

REQUIRE_NO_FAIL(con.Query("CREATE TABLE t1(i INTEGER)"));

auto json = con.GetSubstraitJSON("INSERT INTO t1 VALUES (1), (2), (3), (NULL)");
auto result = con.FromSubstraitJSON(json);
// number of rows inserted are expected as result of insert
REQUIRE(CHECK_COLUMN(result, 0, {4}));
}

TEST_CASE("Test C Get and To Substrait API for Select from virtual table", "[substrait-api]") {
DuckDB db(nullptr);
SubstraitExtension substrait_extension;
substrait_extension.Load(db);
Connection con(db);
con.EnableQueryVerification();


auto json = con.GetSubstrait("SELECT * FROM (VALUES (1, 2), (3, 4))");
auto result = con.FromSubstrait(json);
// number of rows selected are expected as result of insert
REQUIRE(CHECK_COLUMN(result, 0, {1, 3}));
REQUIRE(CHECK_COLUMN(result, 1, {2, 4}));
}

TEST_CASE("Test C Get and To JSON-Substrait API for Select from virtual table", "[substrait-api]") {
DuckDB db(nullptr);
SubstraitExtension substrait_extension;
substrait_extension.Load(db);
Connection con(db);
con.EnableQueryVerification();


auto json = con.GetSubstraitJSON("SELECT * FROM (VALUES (1, 2), (3, 4))");
auto result = con.FromSubstraitJSON(json);
// number of rows selected are expected as result of insert
REQUIRE(CHECK_COLUMN(result, 0, {1, 3}));
REQUIRE(CHECK_COLUMN(result, 1, {2, 4}));
}