Skip to content

Commit e5ae260

Browse files
groeneaiclaude
andcommitted
Forbid to create a table as a MergeTree introspection table function
CREATE TABLE p AS mergeTreeIndex('db', 's') was accepted, and once p had been read it made s undroppable: DROP TABLE s SYNC never returned, s stayed in system.dropped_tables forever, and its data was never reclaimed from disk. The same persisted form is wrong two further ways that were measured alongside: reads of p keep describing the dropped table (count(p) stayed at 2 after the source was dropped and re-created with 3 rows), and p stops seeing new parts of a live source, because the parts vector is captured in the storage's constructor. A persisted table function is wrapped in a StorageTableFunctionProxy, whose getNestedImpl memoises the storage it built in nested and clears get_nested, so the storage lives for the whole server lifetime after the first read. Each of these five storages keeps the source table's StoragePtr as a data member: StorageMergeTreeIndex::source_table, StorageFromMergeTreeProjection::parent_storage, StorageMergeTreeAnalyzeIndexes::source_table, StorageMergeTreeTextIndex::source_table and StorageMergeTreeCodecBlockCounts::source_table. DROP TABLE s SYNC detaches s, pushes it onto tables_marked_dropped and waits in DatabaseCatalog::waitTableFinallyDropped, but the background drop task only takes an entry whose pointer is unique (DatabaseCatalog::getTablesToDrop, bool in_use = it->table && !isSharedPtrUnique(it->table)). The memoised member is a permanent second reference, so in_use stays true, the entry is never selected, and the wait loop exits only on is_shutting_down. The gate is an ordinary shared_ptr use-count test rather than an assertion, so release builds hang identically. Removing the member is not the fix. Four of the five storages also cache MergeTreeData::DataPartsVector data_parts in their constructor, and IMergeTreeDataPart references its storage by raw reference (const MergeTreeData & storage), so the cached parts do not keep the source alive and would dangle. The tree states this in two places: MergeTreeData::SnapshotData carries a ConstStoragePtr storage to compensate, and StorageMergeTreeCodecBlockCounts says "The parts reference the source table's MergeTreeData without owning it" before calling addStorageHolder. Resolving the source per read instead would be a redesign rather than a mechanical change, above all for StorageFromMergeTreeProjection, whose const MergeTreeData & member cannot be rebound, whose projection is a raw interior pointer into the parent metadata, and whose constructor derives the storage's own schema from the source's projection. So the defect is not that the storage holds a pointer, which it must, but that this storage is allowed to outlive its query. canBeUsedToCreateTable() is the mechanism that already expresses exactly that, used by eval, viewIfPermitted and the cluster functions, so give the five table functions the same override. Five class bodies cover six registered names, because mergeTreeAnalyzeIndexes and mergeTreeAnalyzeIndexesUUID are the same class registered twice. Nothing on the drop path changes, no bound is relaxed, and the transient SELECT ... FROM mergeTreeIndex(...) form, the only documented and only in-tree-exercised usage, is untouched. The veto inspects only the outermost function, so nested carriers were measured rather than assumed: remote(self, view(SELECT * FROM mergeTreeIndex(...))), merge() over a database holding the proxy, and ENGINE = Remote(..., mergeTreeIndex(...)) each materialise the inner storage and still release it, because the outer storage keeps the AST and rebuilds the inner one per query. The direct persisted form is therefore the only pinning carrier. canBeUsedToCreateTable() is consulted at two sites and only one is reached by metadata replay, since the inferred column list is itself persisted. A server that already holds such a table therefore keeps loading it at startup and on ATTACH DATABASE, which route through DatabaseOnDisk::createTableFromAST and do not consult the veto, while an explicit ATTACH TABLE, a RESTORE, a replica recovery and CREATE TABLE x AS p are refused. That asymmetry is the same one the existing vetoes already carry, and it applies to a form with no uses anywhere in the tree. Co-Authored-By: Claude Opus 5 <[email protected]>
1 parent 55c2563 commit e5ae260

7 files changed

Lines changed: 42 additions & 0 deletions

‎src/TableFunctions/TableFunctionMergeTreeAnalyzeIndexes.cpp‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ class TableFunctionMergeTreeAnalyzeIndexes : public ITableFunction
8989

9090
std::string getName() const override { return mergeTreeAnalyzeIndexFunctionName(resolve_by_uuid); }
9191

92+
/// The returned storage holds its source table's storage object, so a persisted table would keep the source undroppable.
93+
bool canBeUsedToCreateTable() const override { return false; }
94+
9295
void parseArguments(const ASTPtr & ast_function, ContextPtr context) override;
9396
ColumnsDescription getActualTableStructure(ContextPtr context, bool is_insert_query) const override;
9497
VectorWithMemoryTracking<size_t> skipAnalysisForArguments(const QueryTreeNodePtr & query_node_table_function, ContextPtr context) const override;

‎src/TableFunctions/TableFunctionMergeTreeCodecBlockCounts.cpp‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ class TableFunctionMergeTreeCodecBlockCounts : public ITableFunction
2828
static constexpr auto name = "mergeTreeCodecBlockCounts";
2929
std::string getName() const override { return name; }
3030

31+
/// The returned storage holds its source table's storage object, so a persisted table would keep the source undroppable.
32+
bool canBeUsedToCreateTable() const override { return false; }
33+
3134
void parseArguments(const ASTPtr & ast_function, ContextPtr context) override;
3235
ColumnsDescription getActualTableStructure(ContextPtr context, bool is_insert_query) const override;
3336

‎src/TableFunctions/TableFunctionMergeTreeIndex.cpp‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ class TableFunctionMergeTreeIndex : public ITableFunction
3030
static constexpr auto name = "mergeTreeIndex";
3131
std::string getName() const override { return name; }
3232

33+
/// The returned storage holds its source table's storage object, so a persisted table would keep the source undroppable.
34+
bool canBeUsedToCreateTable() const override { return false; }
35+
3336
void parseArguments(const ASTPtr & ast_function, ContextPtr context) override;
3437
ColumnsDescription getActualTableStructure(ContextPtr context, bool is_insert_query) const override;
3538

‎src/TableFunctions/TableFunctionMergeTreeTextIndex.cpp‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ class TableFunctionMergeTreeTextIndex : public ITableFunction
2828
static constexpr auto name = "mergeTreeTextIndex";
2929
std::string getName() const override { return name; }
3030

31+
/// The returned storage holds its source table's storage object, so a persisted table would keep the source undroppable.
32+
bool canBeUsedToCreateTable() const override { return false; }
33+
3134
void parseArguments(const ASTPtr & ast_function, ContextPtr context) override;
3235
ColumnsDescription getActualTableStructure(ContextPtr context, bool is_insert_query) const override;
3336

‎src/TableFunctions/TableFunctionProjection.cpp‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ class TableFunctionMergeTreeProjection : public ITableFunction
2121
static constexpr auto name = "mergeTreeProjection";
2222
std::string getName() const override { return name; }
2323

24+
/// The returned storage holds its source table's storage object, so a persisted table would keep the source undroppable.
25+
bool canBeUsedToCreateTable() const override { return false; }
26+
2427
void parseArguments(const ASTPtr & ast_function, ContextPtr context) override;
2528
ColumnsDescription getActualTableStructure(ContextPtr context, bool is_insert_query) const override;
2629

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
DROP TABLE IF EXISTS s_05138;
2+
3+
CREATE TABLE s_05138
4+
(
5+
id UInt32,
6+
s String,
7+
INDEX ti(s) TYPE text(tokenizer = 'splitByNonAlpha'),
8+
PROJECTION pj (SELECT s, count() GROUP BY s)
9+
)
10+
ENGINE = MergeTree ORDER BY id;
11+
12+
INSERT INTO s_05138 VALUES (1, 'a');
13+
14+
-- The transient form keeps working.
15+
SELECT count() > 0 FROM mergeTreeIndex(currentDatabase(), 's_05138');
16+
17+
-- A persistent table over one of these functions would hold the source table's storage object
18+
-- forever, so DROP TABLE s_05138 SYNC would never return. Every registered name is refused.
19+
CREATE TABLE p_index_05138 AS mergeTreeIndex(currentDatabase(), 's_05138'); -- { serverError BAD_ARGUMENTS }
20+
CREATE TABLE p_projection_05138 AS mergeTreeProjection(currentDatabase(), 's_05138', 'pj'); -- { serverError BAD_ARGUMENTS }
21+
CREATE TABLE p_analyze_05138 AS mergeTreeAnalyzeIndexes(currentDatabase(), 's_05138', id = 1); -- { serverError BAD_ARGUMENTS }
22+
CREATE TABLE p_text_05138 AS mergeTreeTextIndex(currentDatabase(), 's_05138', 'ti'); -- { serverError BAD_ARGUMENTS }
23+
CREATE TABLE p_codec_05138 AS mergeTreeCodecBlockCounts(currentDatabase(), 's_05138'); -- { serverError BAD_ARGUMENTS }
24+
CREATE TABLE p_analyze_uuid_05138 AS mergeTreeAnalyzeIndexesUUID('00000000-0000-0000-0000-000000000001', id = 1); -- { serverError BAD_ARGUMENTS }
25+
26+
DROP TABLE s_05138;

0 commit comments

Comments
 (0)