From eaa4f01ffaeaee0c964ec040a3fd77fc15c43972 Mon Sep 17 00:00:00 2001 From: Saurabh Kumar Date: Wed, 17 Jul 2024 00:53:05 +0530 Subject: [PATCH] Add Column Header to the exported CSV from query command (#1313) # Issue https://github.com/dbt-labs/metricflow/issues/1285 The CSV exported from the `mf query --csv ` command does not contain the column header. The CSV thus does not make too much sense. # Fix - The code [here](https://github.com/dbt-labs/metricflow/blob/main/dbt-metricflow/dbt_metricflow/cli/main.py#L344) is writing data to the CSV file. The column names aren't getting added to the file. - With this PR, we are adding the column names returned from `df.column_names` to the CSV to fix the issue. --- .changes/unreleased/Fixes-20240716-090114.yaml | 6 ++++++ dbt-metricflow/dbt_metricflow/cli/main.py | 1 + 2 files changed, 7 insertions(+) create mode 100644 .changes/unreleased/Fixes-20240716-090114.yaml diff --git a/.changes/unreleased/Fixes-20240716-090114.yaml b/.changes/unreleased/Fixes-20240716-090114.yaml new file mode 100644 index 0000000000..acbacc51f7 --- /dev/null +++ b/.changes/unreleased/Fixes-20240716-090114.yaml @@ -0,0 +1,6 @@ +kind: Fixes +body: Add Column header to the exported CSV from query command +time: 2024-07-16T09:01:14.102978+05:30 +custom: + Author: saurabh0402 + Issue: 1285,1313 diff --git a/dbt-metricflow/dbt_metricflow/cli/main.py b/dbt-metricflow/dbt_metricflow/cli/main.py index cf05ff9f28..c59ad93435 100644 --- a/dbt-metricflow/dbt_metricflow/cli/main.py +++ b/dbt-metricflow/dbt_metricflow/cli/main.py @@ -340,6 +340,7 @@ def query( elif csv is not None: # csv is a LazyFile that is file-like that works in this case. csv_writer = csv_module.writer(csv) + csv_writer.writerow(df.column_names) for row in df.rows: csv_writer.writerow(row) click.echo(f"🖨 Successfully written query output to {csv.name}")