# Warehouse table

A Parquet [bucket export](/developers/bucket-export) can keep a Delta Lake table over the files it
delivers, so your warehouse reads a workspace's cost as one table, `focus_costs`, that always holds
the newest version of every day. Costfluent writes the statement that registers it in your
warehouse, filled with the destination's own bucket, prefix and workspace, and the SQL behind a
saved report or a dashboard's report widget runs against it with nothing to edit.

## Before you start

- A bucket export destination with **Parquet** under **File format**. Adding or editing one needs
  the rights [Bucket export](/developers/bucket-export#before-you-start) names.
- Access in your warehouse to create a table, and to give it read access to the bucket, as the
  engine's section below names.

## How it works

Costfluent copies no data for the table. After each run that delivers, it writes one commit into a
`_delta_log/` folder under the workspace's feed folder, which removes the parts of each day's
previous version and adds the new version's parts, with the same write-only credential that writes
the files. Your warehouse registers the table once and reads the newest commit on every query, with
read access of its own, which the export never has.

```mermaid The export writes each day's parts and a commit into your bucket with its write-only credential; your warehouse reads the newest commit and the parts it names with its own read access.
flowchart LR
  C[Costfluent export run] -->|writes parts and a commit| B[(Your bucket)]
  W[Your warehouse] -->|reads the newest commit and its parts| B
```

## Turn the table on

<Stepper>

1. Open **Settings**, then **Workspaces**, select the workspace, and find the **Bucket export** card.
2. Select **Add destination**, or edit an existing destination.
3. Choose **Parquet** under **File format**, and turn on **Warehouse table**. For CSV the switch is
   disabled, with "Parquet only."
4. Save. The destination's row shows the table's path, with **Copy table path** and **Connect a
   warehouse**. It says "Warehouse table: waiting for the export run at" the time of the next run,
   in the workspace's time zone, until the first commit lands, and then names the commit.

</Stepper>

<Framed caption="A Parquet destination on the Bucket export card with its warehouse table on.">
  ![The Bucket export card listing a Parquet destination whose row shows its warehouse table, with the Connect a warehouse action](/screenshots/en/warehouse-table-destination.png)
</Framed>

The export runs every 4 hours from midnight UTC, at 15 minutes past. Each run delivers at most 31
days to a destination, so a new destination fills its history over several runs; the table holds a
day once the run that delivers it has committed.

## Connect a warehouse

<Stepper>

1. On the destination's row, select **Connect a warehouse**.
2. Choose the **Warehouse**. An engine that cannot read the destination's storage is shown
   unavailable, with the reason.
3. Give the engine read access as the line under the statement says, and as its section below
   describes.
4. Select **Copy statement** and run the statement in your warehouse. Replace each value in angle
   brackets, such as a BigQuery project and dataset, with your own.

</Stepper>

<Framed caption="Connect a warehouse over a Hetzner destination, with DuckDB, the first engine that reads it.">
  ![The Connect a warehouse dialog with DuckDB chosen, showing the statement for the destination's endpoint and bucket and the Copy statement button](/screenshots/en/warehouse-connect-dialog.png)
</Framed>

The statements below are the dialog's for each storage, with the destination's values as
placeholders. The table's root is the workspace's feed folder,
`<prefix>/costfluent/<workspace ID>/focus-1.4/`.

### BigQuery

BigQuery reads a Google Cloud Storage destination as a BigLake table. It needs a Cloud resource
connection whose service account has Storage Object Viewer on the bucket. BigQuery follows
[Create BigLake external tables for Delta Lake](https://docs.cloud.google.com/bigquery/docs/create-delta-lake-table).

```sql
CREATE EXTERNAL TABLE `<project>.<dataset>.focus_costs`
WITH CONNECTION `<project>.<region>.<connection>`
OPTIONS (
  format = 'DELTA_LAKE',
  uris = ['gs://<bucket>/<prefix>/costfluent/<workspace ID>/focus-1.4']
);
```

### Snowflake

Snowflake reads an AWS S3, Google Cloud Storage or Azure Blob Storage destination through an
external volume over the bucket and a catalog integration for Delta Lake files, and refreshes the
table on its own. The volume is read only. On AWS, name a role Snowflake assumes that may read the
prefix; on Azure, your tenant. Snowflake follows
[CREATE ICEBERG TABLE (Delta files in object storage)](https://docs.snowflake.com/en/sql-reference/sql/create-iceberg-table-delta).

AWS S3:

```sql
CREATE EXTERNAL VOLUME costfluent_volume
  STORAGE_LOCATIONS = (
    (
      NAME = 'costfluent'
      STORAGE_PROVIDER = 'S3'
      STORAGE_BASE_URL = 's3://<bucket>/'
      STORAGE_AWS_ROLE_ARN = '<role ARN Snowflake assumes>'
    )
  )
  ALLOW_WRITES = FALSE;

CREATE CATALOG INTEGRATION costfluent_delta
  CATALOG_SOURCE = OBJECT_STORE
  TABLE_FORMAT = DELTA
  ENABLED = TRUE;

CREATE ICEBERG TABLE focus_costs
  CATALOG = 'costfluent_delta'
  EXTERNAL_VOLUME = 'costfluent_volume'
  BASE_LOCATION = '<prefix>/costfluent/<workspace ID>/focus-1.4/'
  AUTO_REFRESH = TRUE;
```

Google Cloud Storage:

```sql
CREATE EXTERNAL VOLUME costfluent_volume
  STORAGE_LOCATIONS = (
    (
      NAME = 'costfluent'
      STORAGE_PROVIDER = 'GCS'
      STORAGE_BASE_URL = 'gcs://<bucket>/'
    )
  )
  ALLOW_WRITES = FALSE;

CREATE CATALOG INTEGRATION costfluent_delta
  CATALOG_SOURCE = OBJECT_STORE
  TABLE_FORMAT = DELTA
  ENABLED = TRUE;

CREATE ICEBERG TABLE focus_costs
  CATALOG = 'costfluent_delta'
  EXTERNAL_VOLUME = 'costfluent_volume'
  BASE_LOCATION = '<prefix>/costfluent/<workspace ID>/focus-1.4/'
  AUTO_REFRESH = TRUE;
```

Azure Blob Storage:

```sql
CREATE EXTERNAL VOLUME costfluent_volume
  STORAGE_LOCATIONS = (
    (
      NAME = 'costfluent'
      STORAGE_PROVIDER = 'AZURE'
      STORAGE_BASE_URL = 'azure://<storage account>.blob.core.windows.net/<container>/'
      AZURE_TENANT_ID = '<Azure tenant ID>'
    )
  )
  ALLOW_WRITES = FALSE;

CREATE CATALOG INTEGRATION costfluent_delta
  CATALOG_SOURCE = OBJECT_STORE
  TABLE_FORMAT = DELTA
  ENABLED = TRUE;

CREATE ICEBERG TABLE focus_costs
  CATALOG = 'costfluent_delta'
  EXTERNAL_VOLUME = 'costfluent_volume'
  BASE_LOCATION = '<prefix>/costfluent/<workspace ID>/focus-1.4/'
  AUTO_REFRESH = TRUE;
```

### Athena

Athena reads an AWS S3 destination. It needs an AWS Glue database to create the table in, and
`s3:GetObject` and `s3:ListBucket` on the prefix for whoever queries it. Athena follows
[Query Linux Foundation Delta Lake tables](https://docs.aws.amazon.com/athena/latest/ug/delta-lake-tables.html).

```sql
CREATE EXTERNAL TABLE focus_costs
LOCATION 's3://<bucket>/<prefix>/costfluent/<workspace ID>/focus-1.4/'
TBLPROPERTIES ('table_type' = 'DELTA');
```

### Databricks

Databricks reads an AWS S3, Google Cloud Storage or Azure Blob Storage destination as a Unity
Catalog external table. It needs a storage credential that can read the bucket and an external
location over the table's path. Databricks follows
[Work with external tables](https://docs.databricks.com/aws/en/tables/external).

AWS S3:

```sql
-- Needs a Unity Catalog external location over this path, with a storage credential that can read it.
CREATE TABLE <catalog>.<schema>.focus_costs
USING DELTA
LOCATION 's3://<bucket>/<prefix>/costfluent/<workspace ID>/focus-1.4';
```

Google Cloud Storage:

```sql
-- Needs a Unity Catalog external location over this path, with a storage credential that can read it.
CREATE TABLE <catalog>.<schema>.focus_costs
USING DELTA
LOCATION 'gs://<bucket>/<prefix>/costfluent/<workspace ID>/focus-1.4';
```

Azure Blob Storage:

```sql
-- Needs a Unity Catalog external location over this path, with a storage credential that can read it.
CREATE TABLE <catalog>.<schema>.focus_costs
USING DELTA
LOCATION 'abfss://<container>@<storage account>.dfs.core.windows.net/<prefix>/costfluent/<workspace ID>/focus-1.4';
```

### DuckDB

DuckDB reads every storage with its `delta` extension. It needs a secret with read access to the
bucket; the statement creates one and a view `focus_costs` over `delta_scan`. Over Cloud Storage the
secret takes an HMAC key, and over Azure it signs in through the Azure credential chain. DuckDB
follows [Delta Extension](https://duckdb.org/docs/stable/core_extensions/delta).

AWS S3:

```sql
INSTALL delta;
LOAD delta;
CREATE SECRET costfluent (
  TYPE s3,
  KEY_ID '<access key ID>',
  SECRET '<secret access key>',
  REGION '<region>'
);
CREATE VIEW focus_costs AS
SELECT * FROM delta_scan('s3://<bucket>/<prefix>/costfluent/<workspace ID>/focus-1.4');
```

Google Cloud Storage:

```sql
INSTALL delta;
LOAD delta;
CREATE SECRET costfluent (
  TYPE gcs,
  KEY_ID '<HMAC key ID>',
  SECRET '<HMAC secret>'
);
CREATE VIEW focus_costs AS
SELECT * FROM delta_scan('gs://<bucket>/<prefix>/costfluent/<workspace ID>/focus-1.4');
```

Azure Blob Storage:

```sql
INSTALL delta;
LOAD delta;
CREATE SECRET costfluent (
  TYPE azure,
  PROVIDER credential_chain,
  ACCOUNT_NAME '<storage account>'
);
CREATE VIEW focus_costs AS
SELECT * FROM delta_scan('abfss://<container>@<storage account>.dfs.core.windows.net/<prefix>/costfluent/<workspace ID>/focus-1.4');
```

S3-compatible Object Storage:

```sql
INSTALL delta;
LOAD delta;
CREATE SECRET costfluent (
  TYPE s3,
  KEY_ID '<access key ID>',
  SECRET '<secret access key>',
  REGION '<region>',
  ENDPOINT '<endpoint>'
);
CREATE VIEW focus_costs AS
SELECT * FROM delta_scan('s3://<bucket>/<prefix>/costfluent/<workspace ID>/focus-1.4');
```

### ClickHouse

ClickHouse reads an AWS S3, Azure Blob Storage or S3-compatible destination with its `deltaLake`
and `deltaLakeAzure` table functions; the statement creates a view `focus_costs` over one. It needs
credentials with read access to the bucket, in the statement or in a named collection. ClickHouse
follows [deltaLake](https://clickhouse.com/docs/sql-reference/table-functions/deltalake).

AWS S3:

```sql
CREATE VIEW focus_costs AS
SELECT * FROM deltaLake(
  'https://<bucket>.s3.<region>.amazonaws.com/<prefix>/costfluent/<workspace ID>/focus-1.4/',
  '<access key ID>', '<secret access key>');
```

Azure Blob Storage:

```sql
CREATE VIEW focus_costs AS
SELECT * FROM deltaLakeAzure(
  'https://<storage account>.blob.core.windows.net', '<container>', '<prefix>/costfluent/<workspace ID>/focus-1.4/',
  '<storage account>', '<account key>');
```

S3-compatible Object Storage:

```sql
CREATE VIEW focus_costs AS
SELECT * FROM deltaLake(
  'https://<endpoint>/<bucket>/<prefix>/costfluent/<workspace ID>/focus-1.4/',
  '<access key ID>', '<secret access key>');
```

## Check the result

Count the rows and sum `BilledCost` per day and `BillingCurrency`:

```sql
SELECT CAST(ChargePeriodStart AS DATE) AS charge_day, BillingCurrency, COUNT(*) AS row_count, SUM(BilledCost) AS billed_cost
FROM focus_costs
GROUP BY 1, 2
ORDER BY 1 DESC, 2;
```

- The newest day is the one the destination's row says it is delivered through, and a day without
  cost has no row.
- A month's days add up to the same month in a cost report with **Amortized cost** off and no
  filter, when the workspace is billed in one currency and the report does not convert it.

## Virtual tags

The table's last column, `x_VirtualTags`, holds each row's [virtual tags](/allocation/virtual-tags)
as a JSON object of text, such as `{"environment":"production","team":"payments"}`:

- It carries every enabled virtual tag of the workspace, keyed by the tag's key, with the value a
  report gives the row. A draft is not exported until you publish it.
- Every exported key is on every row. Where no provider tag key, rule or default value gives the row
  a value, the value is empty text, the row a report shows as "No" and the tag's key.
- A workspace without an enabled virtual tag writes `{}`.
- An older virtual tag whose rule still reads another virtual tag is left out while that tag is
  deleted or disabled, since reports cannot read it then either.

Saving a virtual tag, publishing its draft, enabling a template or deleting a tag changes what the
column holds, so the export delivers every day of the history again over the next runs. Saving a
draft changes nothing. Each run still delivers the days whose cost changed first, oldest first, and
then refreshes the other days, newest first, within the days a run delivers, so new cost keeps
arriving while the history refreshes. A refresh alone never holds back the day the destination's row
says "Delivered through", nor makes it behind. While days are left to refresh, the row's
**Delivery** column also says "Updating virtual tags: 12 days left", counting the days the last run
left; the line goes once a run leaves none. Until a day is refreshed, the table holds its values
under the definitions it was last delivered with, so a query over it can differ from a report. Days
delivered before the files carried `x_VirtualTags` are delivered once more the same way; until
then, their parts lack the column and an engine reads it as null on their rows.

Each refreshed day is a new `version=` folder written in full, and the folder it replaces stays in
the bucket, since the export's credential only creates objects: every change to the virtual tags
adds another copy of the history to the bucket. A day's folder is replaced once a newer `version=`
folder of the same day holds a `manifest.json`; only those older folders are safe to delete (see
[Versions](/developers/bucket-export#versions)). A lifecycle rule that deletes by age is not safe,
since an old day's only version is old too and the table still names it.

Read one tag, here `team`, in the engine that holds the table; replace `team` with your tag's key.

BigQuery:

```sql
SELECT JSON_VALUE(`x_VirtualTags`, '$."team"') AS team, SUM(`BilledCost`) AS billed_cost
FROM focus_costs
GROUP BY 1
ORDER BY 2 DESC;
```

Snowflake:

```sql
SELECT PARSE_JSON("x_VirtualTags"):"team"::STRING AS team, SUM("BilledCost") AS billed_cost
FROM focus_costs
GROUP BY 1
ORDER BY 2 DESC;
```

Athena:

```sql
SELECT json_extract_scalar("x_VirtualTags", '$["team"]') AS team, SUM("BilledCost") AS billed_cost
FROM focus_costs
GROUP BY 1
ORDER BY 2 DESC;
```

Databricks:

```sql
SELECT get_json_object(x_VirtualTags, '$["team"]') AS team, SUM(BilledCost) AS billed_cost
FROM focus_costs
GROUP BY 1
ORDER BY 2 DESC;
```

DuckDB:

```sql
SELECT json_extract_string("x_VirtualTags", '$."team"') AS team, SUM("BilledCost") AS billed_cost
FROM focus_costs
GROUP BY 1
ORDER BY 2 DESC;
```

ClickHouse:

```sql
SELECT JSONExtractString("x_VirtualTags", 'team') AS team, SUM("BilledCost") AS billed_cost
FROM focus_costs
GROUP BY 1
ORDER BY 2 DESC;
```

In BigQuery, qualify `focus_costs` with your dataset. Keys are case-sensitive, so `team` and `Team`
can be two virtual tags; Databricks matches the case only in the bracket form `$["team"]`, not in
`$.team`, as [get_json_object](https://docs.databricks.com/aws/en/sql/language-manual/functions/get_json_object)
says.

## Copy SQL

**Copy SQL** under **Export** on a saved report writes the report's query over the table, for
BigQuery, Snowflake, Athena, DuckDB or ClickHouse; see [Copy SQL](/cost-reports/explore#copy-sql).
A report widget on a dashboard has **Copy SQL** too, which writes its query as the dashboard shows
it; see [Copy a widget's SQL](/reporting/dashboards#copy-a-widgets-sql).
The query reads `focus_costs`, the name every statement above gives the table, so it runs unchanged
against a table registered from **Connect a warehouse**; in BigQuery, qualify it with your dataset.
Copy SQL writes no Databricks query: Databricks reads the same table, so write your own over its
columns. A report grouped or filtered by a virtual tag reads the tag's key from `x_VirtualTags`,
with each day's values as of that day's last delivery (see [Virtual tags](#virtual-tags)). A report
using a segment gets no SQL: segments are Costfluent's own allocation, which the table does not
carry.

## Troubleshooting

- **The table reads empty or is not found.** Until the first run commits, there is no
  `_delta_log/` folder yet; the destination's row says "Warehouse table: waiting for the export run
  at" a time until then.
- **Access denied.** The error comes from your warehouse's own read access to the bucket, not from
  the export, whose credential only writes. Check the access the engine's section names.
- **Days are missing at the start.** Days older than the first day the plan's history keeps are not
  delivered again when the table is turned on, so the table starts there; their files stay in their
  folders.
- **A query fails on a missing file.** A lifecycle rule or a person deleted a version folder or
  `_delta_log/` that the table still names; see the caution under
  [How it behaves](#how-it-behaves). Keep lifecycle rules that delete objects off the workspace's
  feed folder.
- **BigQuery refuses the connection.** BigQuery reads through a connection in the location of the
  bucket and the dataset; create the connection in that region.
- **Snowflake shows an older day.** Snowflake polls the bucket for new commits on its own, by
  default every 30 seconds; `ALTER ICEBERG TABLE focus_costs REFRESH` reads the newest commit at
  once.

## How it behaves

- When a destination's runs have not written for a day,
  the organization roles Owner and Integration Owner are emailed the reason, once for each such
  streak; a run that writes, a move or a new credential ends the streak. An Azure destination's SAS is announced to them once, 14 days before
  it expires.
- A day delivered before the destination could keep a table is delivered once more, under a newer
  version, when the table is turned on, so the table can name its files. Days older than the first
  day the plan's history keeps are not delivered again and stay in their folders only.
- Turned off, the table stops moving: Costfluent writes no more commits, and an engine reads the
  table as of its last one. Turned on again, the next run starts a new log above the old one. The
  log folder is never deleted, like the version folders.
- One folder holds one table: while a destination of the workspace keeps a table, another
  destination of it writing to the same bucket and prefix cannot turn its own on.
- The table holds the columns of [the files](/developers/bucket-export#the-files), typed as the
  Parquet parts are.

:::caution
The table names the parts of each day's newest version. Delete only version folders a newer version
of their day replaced, and never `_delta_log/`, or the engines fail to read the files the table still
names.
:::

## From your assistant

With the [Costfluent MCP server](/developers/mcp) connected, your assistant answers these with `get_cost_report_sql`.

```text
Write the Snowflake SQL behind our saved report "Monthly cost by service" over focus_costs.
```

```text
Why can our saved report "Team showback" not be written as ClickHouse SQL over the warehouse table?
```

## Related

- [Bucket export](/developers/bucket-export)
- [Copy SQL](/cost-reports/explore#copy-sql)
- [Copy a widget's SQL](/reporting/dashboards#copy-a-widgets-sql)
- [Analyse Costfluent cost in your BI tool or warehouse](/guides/cost-data-in-your-bi-tool)
