Skip to content

Commit

Permalink
add some tests of duckdb_execute_prepared_streaming
Browse files Browse the repository at this point in the history
  • Loading branch information
Mause authored and Tmonster committed Dec 7, 2023
1 parent 58b6664 commit 5ee902a
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions test/api/capi/test_capi_prepared.cpp
Expand Up @@ -370,3 +370,61 @@ TEST_CASE("Test prepared statements with named parameters in C API", "[capi]") {

duckdb_destroy_prepare(&stmt);
}

TEST_CASE("Prepared streaming result", "[capi]") {
CAPITester tester;

// open the database in in-memory mode
REQUIRE(tester.OpenDatabase(nullptr));

SECTION("non streaming result") {
REQUIRE(tester.Query("CREATE TABLE t2 (i INTEGER, j INTEGER);"));

duckdb_prepared_statement stmt;
REQUIRE(duckdb_prepare(tester.connection,
"INSERT INTO t2 SELECT 2 AS i, 3 AS j RETURNING *, i * j AS i_times_j",
&stmt) == DuckDBSuccess);
duckdb_result res;
REQUIRE(duckdb_execute_prepared_streaming(stmt, &res) == DuckDBSuccess);
REQUIRE(!duckdb_result_is_streaming(res));
duckdb_destroy_result(&res);
duckdb_destroy_prepare(&stmt);
}

SECTION("streaming result") {
duckdb_prepared_statement stmt;
REQUIRE(duckdb_prepare(tester.connection, "FROM RANGE(0, 10)", &stmt) == DuckDBSuccess);

duckdb_result res;
REQUIRE(duckdb_execute_prepared_streaming(stmt, &res) == DuckDBSuccess);
REQUIRE(duckdb_result_is_streaming(res));

duckdb_data_chunk chunk;
chunk = duckdb_stream_fetch_chunk(res);
REQUIRE(duckdb_data_chunk_get_size(chunk) == 10);

auto vec = duckdb_data_chunk_get_vector(chunk, 0);
auto column_type = duckdb_vector_get_column_type(vec);
REQUIRE(duckdb_get_type_id(column_type) == DUCKDB_TYPE_BIGINT);
duckdb_destroy_logical_type(&column_type);

auto data = (int64_t *)duckdb_vector_get_data(vec);
REQUIRE(data[0] == 0);
REQUIRE(data[1] == 1);
REQUIRE(data[2] == 2);
REQUIRE(data[3] == 3);
REQUIRE(data[4] == 4);
REQUIRE(data[5] == 5);
REQUIRE(data[6] == 6);
REQUIRE(data[7] == 7);
REQUIRE(data[8] == 8);
REQUIRE(data[9] == 9);

duckdb_destroy_data_chunk(&chunk);

REQUIRE(duckdb_stream_fetch_chunk(res) == nullptr);

duckdb_destroy_result(&res);
duckdb_destroy_prepare(&stmt);
}
}

0 comments on commit 5ee902a

Please sign in to comment.