Repository: duckdb/duckdb
File: src/main/query_profiler.cpp (C++)

Code before:
	}

	query_metrics.latency.End();
	if (root) {
		auto &info = root->GetProfilingInfo();
		if (info.Enabled(info.expanded_settings, MetricsType::OPERATOR_CARDINALITY)) {
			Finalize(*root->GetChild(0));
		}
	}
	running = false;
	bool emit_output = false;

	// Print or output the query profiling after query termination.
	// EXPLAIN ANALYZE output is not written by the profiler.
	if (IsEnabled() && !is_explain_analyze) {
		if (root) {
			auto &info = root->GetProfilingInfo();
			info = ProfilingInfo(ClientConfig::GetConfig(context).profiler_settings);
			auto &child_info = root->children[0]->GetProfilingInfo();

			auto &settings = info.expanded_settings;
			if (info.Enabled(settings, MetricsType::QUERY_NAME)) {
				info.metrics[MetricsType::QUERY_NAME] = query_metrics.query;
			}
			for (const auto &global_info_entry : query_metrics.query_global_info.metrics) {
				if (info.Enabled(settings, global_info_entry.first)) {
					info.metrics[global_info_entry.first] = global_info_entry.second;
				}
			}
			if (info.Enabled(settings, MetricsType::LATENCY)) {
				info.metrics[MetricsType::LATENCY] = query_metrics.latency.Elapsed();
			}
			if (info.Enabled(settings, MetricsType::TOTAL_BYTES_READ)) {
				info.metrics[MetricsType::TOTAL_BYTES_READ] = Value::UBIGINT(query_metrics.total_bytes_read);
			}
			if (info.Enabled(settings, MetricsType::TOTAL_BYTES_WRITTEN)) {
				info.metrics[MetricsType::TOTAL_BYTES_WRITTEN] = Value::UBIGINT(query_metrics.total_bytes_written);
			}
			if (info.Enabled(settings, MetricsType::TOTAL_MEMORY_ALLOCATED)) {
				info.metrics[MetricsType::TOTAL_MEMORY_ALLOCATED] =
				    Value::UBIGINT(query_metrics.total_memory_allocated);
			}
			if (info.Enabled(settings, MetricsType::ROWS_RETURNED)) {
				info.metrics[MetricsType::ROWS_RETURNED] = child_info.metrics[MetricsType::OPERATOR_CARDINALITY];
			}
			if (info.Enabled(settings, MetricsType::CPU_TIME)) {
				GetCumulativeMetric<double>(*root, MetricsType::CPU_TIME, MetricsType::OPERATOR_TIMING);
			}
			if (info.Enabled(settings, MetricsType::CUMULATIVE_CARDINALITY)) {
				GetCumulativeMetric<idx_t>(*root, MetricsType::CUMULATIVE_CARDINALITY,
				                           MetricsType::OPERATOR_CARDINALITY);

Review comment (refactor):
I was hesitant to add this, but this feels like the right thing to do (add only when the metric is explicitly enabled).

@maiadegraaf - Please let me know if you want me to remove this.

Code after:
	}

	query_metrics.latency.End();
	if (root) {
		auto &info = root->GetProfilingInfo();
		if (info.Enabled(info.expanded_settings, MetricsType::OPERATOR_CARDINALITY)) {
			Finalize(*root->GetChild(0));
		}
	}
	running = false;
	bool emit_output = false;

	// Print or output the query profiling after query termination.
	// EXPLAIN ANALYZE output is not written by the profiler.
	if (IsEnabled() && !is_explain_analyze) {
		if (root) {
			auto &info = root->GetProfilingInfo();
			info = ProfilingInfo(ClientConfig::GetConfig(context).profiler_settings);
			auto &child_info = root->children[0]->GetProfilingInfo();

			auto &settings = info.expanded_settings;
			if (info.Enabled(settings, MetricsType::QUERY_NAME)) {
				info.metrics[MetricsType::QUERY_NAME] = query_metrics.query;
			}
			for (const auto &global_info_entry : query_metrics.query_global_info.metrics) {
				info.metrics[global_info_entry.first] = global_info_entry.second;
			}
			if (info.Enabled(settings, MetricsType::LATENCY)) {
				info.metrics[MetricsType::LATENCY] = query_metrics.latency.Elapsed();
			}
			if (info.Enabled(settings, MetricsType::TOTAL_BYTES_READ)) {
				info.metrics[MetricsType::TOTAL_BYTES_READ] = Value::UBIGINT(query_metrics.total_bytes_read);
			}
			if (info.Enabled(settings, MetricsType::TOTAL_BYTES_WRITTEN)) {
				info.metrics[MetricsType::TOTAL_BYTES_WRITTEN] = Value::UBIGINT(query_metrics.total_bytes_written);
			}
			if (info.Enabled(settings, MetricsType::TOTAL_MEMORY_ALLOCATED)) {
				info.metrics[MetricsType::TOTAL_MEMORY_ALLOCATED] =
				    Value::UBIGINT(query_metrics.total_memory_allocated);
			}
			if (info.Enabled(settings, MetricsType::ROWS_RETURNED)) {
				info.metrics[MetricsType::ROWS_RETURNED] = child_info.metrics[MetricsType::OPERATOR_CARDINALITY];
			}
			if (info.Enabled(settings, MetricsType::CPU_TIME)) {
				GetCumulativeMetric<double>(*root, MetricsType::CPU_TIME, MetricsType::OPERATOR_TIMING);
			}
			if (info.Enabled(settings, MetricsType::CUMULATIVE_CARDINALITY)) {
				GetCumulativeMetric<idx_t>(*root, MetricsType::CUMULATIVE_CARDINALITY,
				                           MetricsType::OPERATOR_CARDINALITY);
			}
			if (info.Enabled(settings, MetricsType::CUMULATIVE_ROWS_SCANNED)) {