Repository: duckdb/duckdb
File: test/api/capi/capi_table_functions.cpp (C++)

Code before:
		if (static_cast<idx_t>(init_data->pos) >= bind_data->rows_requested) {
			break;
		}
		ptr[i] = bind_data->connection_id;
		ptr2[i] = 42;
		init_data->pos++;
	}
	duckdb_data_chunk_set_size(output, i);
}

TEST_CASE("Table function client context return") {
	CAPITester tester;
	duckdb::unique_ptr<CAPIResult> result;

	REQUIRE(tester.OpenDatabase(nullptr));
	capi_register_table_function(tester.connection, "my_connection_id_function", my_bind_connection_id,
	                             my_init_connection_id, my_function_connection_id);

	duckdb_client_context context;
	duckdb_connection_get_client_context(tester.connection, &context);
	auto first_conn_id = duckdb_client_context_get_connection_id(context);
	duckdb_destroy_client_context(&context);

	result = tester.Query("SELECT * FROM my_connection_id_function(3)");
	REQUIRE_NO_FAIL(*result);
	REQUIRE(result->Fetch<int64_t>(0, 0) == static_cast<int64_t>(first_conn_id));
	REQUIRE(result->Fetch<int64_t>(0, 1) == static_cast<int64_t>(first_conn_id));
	REQUIRE(result->Fetch<int64_t>(0, 2) == static_cast<int64_t>(first_conn_id));
	REQUIRE(result->Fetch<int64_t>(1, 0) == 42);
	REQUIRE(result->Fetch<int64_t>(1, 1) == 42);
	REQUIRE(result->Fetch<int64_t>(1, 2) == 42);
}


Review comment (suggestion):
```sh
/home/vscode/duckdb/test/api/capi/capi_table_functions.cpp:316:46: warning: comparison of integer expressions of different signedness: 'int64_t' {aka 'long int'} and 'long unsigned int' [-Wsign-compare]
  316 |         REQUIRE(result->Fetch<int64_t>(0, 0) == first_conn_id);
      |                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~
/home/vscode/duckdb/test/api/capi/capi_table_functions.cpp:317:46: warning: comparison of integer expressions of different signedness: 'int64_t' {aka 'long int'} and 'long unsigned int' [-Wsign-compare]
  317 |         REQUIRE(result->Fetch<int64_t>(0, 1) == first_conn_id);
      |                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~
/home/vscode/duckdb/test/api/capi/capi_table_functions.cpp:318:46: warning: comparison of integer expressions of different signedness: 'int64_t' {aka 'long int'} and 'long unsigned int' [-Wsign-compare]
  318 |         REQUIRE(result->Fetch<int64_t>(0, 2) == first_conn_id);
      |                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~
```

Code after:
	idx_t i;
	for (i = 0; i < STANDARD_VECTOR_SIZE; i++) {
		if (NumericCast<idx_t>(init_data->pos) >= bind_data->rows_requested) {
			break;
		}
		ptr[i] = bind_data->connection_id;
		ptr2[i] = 42;
		init_data->pos++;
	}
	duckdb_data_chunk_set_size(output, i);
}

TEST_CASE("Table function client context return") {
	CAPITester tester;
	duckdb::unique_ptr<CAPIResult> result;

	REQUIRE(tester.OpenDatabase(nullptr));
	capi_register_table_function(tester.connection, "my_connection_id_function", my_bind_connection_id,
	                             my_init_connection_id, my_function_connection_id);

	duckdb_client_context context;
	duckdb_connection_get_client_context(tester.connection, &context);
	auto first_conn_id = duckdb_client_context_get_connection_id(context);
	duckdb_destroy_client_context(&context);

	result = tester.Query("SELECT * FROM my_connection_id_function(3)");
	REQUIRE_NO_FAIL(*result);
	REQUIRE(result->Fetch<int64_t>(0, 0) == NumericCast<int64_t>(first_conn_id));
	REQUIRE(result->Fetch<int64_t>(0, 1) == NumericCast<int64_t>(first_conn_id));
	REQUIRE(result->Fetch<int64_t>(0, 2) == NumericCast<int64_t>(first_conn_id));
	REQUIRE(result->Fetch<int64_t>(1, 0) == 42);
	REQUIRE(result->Fetch<int64_t>(1, 1) == 42);
	REQUIRE(result->Fetch<int64_t>(1, 2) == 42);
}