> ## Documentation Index
> Fetch the complete documentation index at: https://clickhouse.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# How to store the source file name and path when ingesting with S3Queue

> Keep the name and path of the source file for every row ingested through an S3Queue table by selecting the _path and _file virtual columns in the materialized view.

An [`S3Queue`](https://clickhouse.com/docs/reference/engines/table-engines/integrations/s3queue) table has the virtual columns `_path` and `_file`. To store them with every ingested row, name them in the `SELECT` of the materialized view that reads from the queue, and insert them into ordinary columns of the target table. They are not part of `SELECT *`, which is why a view defined with `SELECT *` leaves such columns empty.

## Example

The example uses the `unordered` mode, in which `S3Queue` tracks the processed files in ClickHouse Keeper, so the server needs a Keeper (or ZooKeeper) configuration. Replace the bucket URL and the credentials with your own.

```sql theme={null}
CREATE TABLE s3_events_queue
(
    id UInt64,
    message String
)
ENGINE = S3Queue(
    'https://<bucket>.s3.<region>.amazonaws.com/events/*.csv',
    '<access-key-id>',
    '<secret-access-key>',
    'CSV')
SETTINGS mode = 'unordered';

CREATE TABLE events
(
    id UInt64,
    message String,
    source_path String,
    source_file String
)
ENGINE = MergeTree
ORDER BY id;

CREATE MATERIALIZED VIEW s3_events_mv TO events AS
SELECT
    id,
    message,
    _path AS source_path,
    _file AS source_file
FROM s3_events_queue;
```

`source_path` and `source_file` are regular columns of `events`: they are returned by `SELECT *` and can be used like any other column. Once files are ingested, you can for example count the rows that came from each file:

```sql theme={null}
SELECT
    source_file,
    count() AS row_count
FROM events
GROUP BY source_file
ORDER BY source_file;
```

`_path` is the path of the file and `_file` is its name, the part of the path after the last `/`. The engine has more virtual columns, such as `_size` and `_time`. See the list of [virtual columns](https://clickhouse.com/docs/reference/engines/table-engines/integrations/s3queue.md#virtual-columns).

## Why the columns are empty with `SELECT *`

`SELECT *` never returns [virtual columns](https://clickhouse.com/docs/reference/engines/table-engines/index.md#table_engines-virtual_columns). A view written as follows selects only `id` and `message`:

```sql theme={null}
CREATE MATERIALIZED VIEW s3_events_mv TO events AS
SELECT *
FROM s3_events_queue;
```

The rows are still inserted, but `source_path` and `source_file` get their default value, an empty string. If you want to keep the asterisk, add the virtual columns next to it:

```sql theme={null}
CREATE MATERIALIZED VIEW s3_events_mv TO events AS
SELECT
    *,
    _path AS source_path,
    _file AS source_file
FROM s3_events_queue;
```

## Add the columns to an existing pipeline

If the queue, the target table and the view already exist, add the columns to the target table and change the query of the view with [`ALTER TABLE ... MODIFY QUERY`](https://clickhouse.com/docs/reference/statements/alter/view):

```sql theme={null}
ALTER TABLE events
    ADD COLUMN source_path String,
    ADD COLUMN source_file String;

ALTER TABLE s3_events_mv
    MODIFY QUERY
    SELECT
        id,
        message,
        _path AS source_path,
        _file AS source_file
    FROM s3_events_queue;
```

`S3Queue` does not read a file again while the file is recorded as processed, so the rows that were ingested before the change keep empty values in the new columns.

In the `unordered` mode that record is limited. ClickHouse Keeper keeps the records of at most [`tracked_files_limit`](https://clickhouse.com/docs/reference/engines/table-engines/integrations/s3queue#tracked_files_limit) processed files (1000 by default) and, if [`tracked_file_ttl_sec`](https://clickhouse.com/docs/reference/engines/table-engines/integrations/s3queue#tracked_file_ttl_sec) is set, only for that many seconds. A file that is still in the bucket when its record is removed is read again, and its rows are inserted a second time. To prevent this, use the [`after_processing`](https://clickhouse.com/docs/reference/engines/table-engines/integrations/s3queue#after_processing) setting to delete or move the files once they are ingested, or set `tracked_files_limit` higher than the number of files that the bucket will ever hold.

## Related pages

* [`S3Queue` table engine](https://clickhouse.com/docs/reference/engines/table-engines/integrations/s3queue)
* [Virtual columns of table engines](https://clickhouse.com/docs/reference/engines/table-engines/index.md#table_engines-virtual_columns)
* [`ALTER TABLE ... MODIFY QUERY`](https://clickhouse.com/docs/reference/statements/alter/view)
