Skip to content

Commit

Permalink
fix: TransformLiteralToValue to check decimal bytes length in from_su…
Browse files Browse the repository at this point in the history
…bstrait (#146)
  • Loading branch information
scgkiran authored Feb 13, 2025
1 parent a1b341c commit be1f02e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/from_substrait.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ Value TransformLiteralToValue(const substrait::Expression_Literal &literal) {
return {literal.string()};
case substrait::Expression_Literal::LiteralTypeCase::kDecimal: {
const auto &substrait_decimal = literal.decimal();
if (substrait_decimal.value().size() != 16) {
throw InvalidInputException("Decimal value must have 16 bytes, but has " +
std::to_string(substrait_decimal.value().size()));
}
auto raw_value = reinterpret_cast<const uint64_t *>(substrait_decimal.value().c_str());
hugeint_t substrait_value {};
substrait_value.lower = raw_value[0];
Expand Down
13 changes: 10 additions & 3 deletions test/c/test_substrait_c_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,20 +341,19 @@ TEST_CASE("Test C VirtualTable input Literal", "[substrait-api]") {

auto json = GetSubstraitJSON(con,"select * from (values (1, 2),(3, 4))");
REQUIRE(!json.empty());
std::cout << json << std::endl;

auto result = FromSubstraitJSON(con,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);

auto json = GetSubstraitJSON(con,"select * from (values (1+1,2+2),(3+3,4+4)) as temp(a,b)");
REQUIRE(!json.empty());
std::cout << json << std::endl;

auto result = FromSubstraitJSON(con,json);
REQUIRE(CHECK_COLUMN(result, 0, {2, 6}));
Expand Down Expand Up @@ -701,7 +700,15 @@ TEST_CASE("Test C Project on empty virtual table for SELECT 1", "[substrait-api]
DuckDB db(nullptr);
Connection con(db);

auto json_str = R"({"version":{"minorNumber":29, "producer":"substrait-go"}, "relations":[{"root":{"input":{"project":{"common":{"direct":{}}, "input":{"read":{"common":{"direct":{}}, "baseSchema":{"struct":{"nullability":"NULLABILITY_REQUIRED"}}, "virtualTable":{"expressions":[{}]}}}, "expressions":[{"literal":{"decimal":{"value":"AQ==", "precision":1}, "nullable":true}}]}}, "names":["?column?"]}}]})";
auto json_str = R"({"version":{"minorNumber":29, "producer":"substrait-go"}, "relations":[{"root":{"input":{"project":{"common":{"direct":{}}, "input":{"read":{"common":{"direct":{}}, "baseSchema":{"struct":{"nullability":"NULLABILITY_REQUIRED"}}, "virtualTable":{"expressions":[{}]}}}, "expressions":[{"literal":{"decimal":{"value":"AQAAAAAAAAAAAAAAAAAAAA==", "precision":1}, "nullable":true}}]}}, "names":["?column?"]}}]})";
auto result = FromSubstraitJSON(con,json_str);
REQUIRE(CHECK_COLUMN(result, 0, {1}));
}

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

auto json_plan = R"({"version":{"minorNumber":29, "producer":"substrait-go"}, "relations":[{"root":{"input":{"project":{"common":{"direct":{}}, "input":{"read":{"common":{"direct":{}}, "baseSchema":{"struct":{"nullability":"NULLABILITY_REQUIRED"}}, "virtualTable":{"expressions":[{}]}}}, "expressions":[{"literal":{"decimal":{"value":"AQ==", "precision":1}, "nullable":true}}]}}, "names":["?column?"]}}]})";
REQUIRE_THROWS(FromSubstraitJSON(con,json_plan));
}

0 comments on commit be1f02e

Please sign in to comment.