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

Code before:
		auto value_c_str = duckdb_get_varchar(value);
		auto value_str = duckdb::string(value_c_str);

		if (key_str == EnumUtil::ToString(MetricsType::EXTRA_INFO)) {
			REQUIRE(value_str.find("__order_by__"));
			REQUIRE(value_str.find("ASC"));
			found_extra_info = true;
		}

		if (key) {
			duckdb_destroy_value(&key);
			duckdb_free(key_c_str);
		}
		if (value) {
			duckdb_destroy_value(&value);
			duckdb_free(value_c_str);
		}
	}

	REQUIRE(found_extra_info);

	duckdb_destroy_value(&map);
	tester.Cleanup();
}

TEST_CASE("Test profiling with the appender", "[capi]") {
	CAPITester tester;
	duckdb::unique_ptr<CAPIResult> result;
	REQUIRE(tester.OpenDatabase(nullptr));

	tester.Query("CREATE TABLE tbl (i INT PRIMARY KEY, value VARCHAR)");
	tester.Query("INSERT INTO tbl VALUES (1, 'hello')");
	REQUIRE_NO_FAIL(tester.Query("PRAGMA enable_profiling = 'no_output'"));
	duckdb_appender appender;

	string query = "INSERT OR REPLACE INTO tbl SELECT i, val FROM my_appended_data";
	duckdb_logical_type types[2];
	types[0] = duckdb_create_logical_type(DUCKDB_TYPE_INTEGER);
	types[1] = duckdb_create_logical_type(DUCKDB_TYPE_VARCHAR);

	const char *column_names[2];
	column_names[0] = "i";
	column_names[1] = "val";

	idx_t column_count = 2;

	auto status = duckdb_appender_create_query(tester.connection, query.c_str(), column_count, types,
	                                           "my_appended_data", column_names, &appender);
	duckdb_destroy_logical_type(&types[0]);
	duckdb_destroy_logical_type(&types[1]);
	REQUIRE(status == DuckDBSuccess);

Review comment (suggestion):
Let's strip this test down a bit, and add another test that uses the non-query appender.
- remove `tester.Query("INSERT INTO tbl VALUES (1, 'hello')");`
- use the default column names
- only append one row

Code after:
		auto value_c_str = duckdb_get_varchar(value);
		auto value_str = duckdb::string(value_c_str);

		if (key_str == EnumUtil::ToString(MetricsType::EXTRA_INFO)) {
			REQUIRE(value_str.find("__order_by__"));
			REQUIRE(value_str.find("ASC"));
			found_extra_info = true;
		}

		if (key) {
			duckdb_destroy_value(&key);
			duckdb_free(key_c_str);
		}
		if (value) {
			duckdb_destroy_value(&value);
			duckdb_free(value_c_str);
		}
	}

	REQUIRE(found_extra_info);

	duckdb_destroy_value(&map);
	tester.Cleanup();
}

TEST_CASE("Test profiling with the appender", "[capi]") {
	CAPITester tester;
	duckdb::unique_ptr<CAPIResult> result;
	REQUIRE(tester.OpenDatabase(nullptr));

	tester.Query("CREATE TABLE tbl (i INT PRIMARY KEY, value VARCHAR)");
	REQUIRE_NO_FAIL(tester.Query("PRAGMA enable_profiling = 'no_output'"));
	REQUIRE_NO_FAIL(tester.Query("SET profiling_coverage='ALL'"));
	duckdb_appender appender;

	string query = "INSERT INTO tbl FROM my_appended_data";
	duckdb_logical_type types[2];
	types[0] = duckdb_create_logical_type(DUCKDB_TYPE_INTEGER);
	types[1] = duckdb_create_logical_type(DUCKDB_TYPE_VARCHAR);

	auto status = duckdb_appender_create_query(tester.connection, query.c_str(), 2, types, "my_appended_data", nullptr,
	                                           &appender);
	duckdb_destroy_logical_type(&types[0]);
	duckdb_destroy_logical_type(&types[1]);
	REQUIRE(status == DuckDBSuccess);
	REQUIRE(duckdb_appender_error(appender) == nullptr);

	REQUIRE(duckdb_appender_begin_row(appender) == DuckDBSuccess);
	REQUIRE(duckdb_append_int32(appender, 1) == DuckDBSuccess);
	REQUIRE(duckdb_append_varchar(appender, "hello world") == DuckDBSuccess);
	REQUIRE(duckdb_appender_end_row(appender) == DuckDBSuccess);