Blog· Tested on ClickHouse 26.4

Which table did this row come from?

I asked for a _table_name virtual column to stop hand-typing table names in UNION ALL reports. It was a duplicate of a request for _table on every table, which shipped in 25.8.

Source: github.com

Comparing tables of the same shape means a UNION ALL with the table name typed as a string literal in each branch, and a copy-pasted branch with a stale label gives a report that lies. I asked for a _table_name virtual column so the label comes from the table itself.

A community member called it a duplicate the same day, and I closed it the next. The original is #60300, opened by alexey-milovidov in February 2024: make _table work on every table, not just Merge tables. PR #63665 by wudidapaopao implemented it, merged 2025-07-29, released in 25.8.

So the feature exists under an older, better-fitting name (_table, next to _database and _part), not _table_name. Virtual columns are not in SELECT *, and next to an aggregate you must group by it:

CREATE TABLE a (x UInt8) ENGINE = MergeTree ORDER BY x;
CREATE TABLE b (x UInt8) ENGINE = MergeTree ORDER BY x;
INSERT INTO a VALUES (1),(2);
INSERT INTO b VALUES (3);

SELECT _table AS t, count() AS cnt FROM a GROUP BY _table
UNION ALL
SELECT _table, count() FROM b GROUP BY _table;
a   2
b   1

(Row order of a UNION ALL is not guaranteed.)

The lesson is about the tracker: search before filing, and a duplicate means someone on the other side already wanted it.

feature-requestvirtual-columnssql-syntax

Discussion

Comments live in GitHub Discussions. Sign in with GitHub to join in.