forked from ClickHouse/ClickHouse
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathStorageAlias.cpp
More file actions
883 lines (721 loc) · 29.7 KB
/
Copy pathStorageAlias.cpp
File metadata and controls
883 lines (721 loc) · 29.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
#include <Storages/StorageAlias.h>
#include <Storages/StorageFactory.h>
#include <Storages/checkAndGetLiteralArgument.h>
#include <Interpreters/DatabaseCatalog.h>
#include <Interpreters/Context.h>
#include <Interpreters/evaluateConstantExpression.h>
#include <Interpreters/InsertDeduplication.h>
#include <Interpreters/InterpreterInsertQuery.h>
#include <Parsers/ASTCreateQuery.h>
#include <Parsers/ASTInsertQuery.h>
#include <QueryPipeline/Pipe.h>
#include <QueryPipeline/BlockIO.h>
#include <QueryPipeline/QueryPlanResourceHolder.h>
#include <Processors/QueryPlan/QueryPlan.h>
#include <Processors/Sinks/SinkToStorage.h>
#include <Processors/Executors/PushingPipelineExecutor.h>
#include <Core/Settings.h>
#include <Access/Common/AccessFlags.h>
#include <Access/ContextAccess.h>
#include <Common/assert_cast.h>
#include <Common/Exception.h>
namespace DB
{
namespace Setting
{
extern const SettingsSeconds lock_acquire_timeout;
}
namespace ErrorCodes
{
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
extern const int BAD_ARGUMENTS;
extern const int NOT_IMPLEMENTED;
}
StorageAlias::StorageAlias(
const StorageID & table_id_,
ContextPtr context_,
const String & target_database_,
const String & target_table_)
: IStorage(table_id_)
, WithContext(context_->getGlobalContext())
, target_database(target_database_)
, target_table(target_table_)
{
StorageID target_id(target_database, target_table);
if (table_id_ == target_id)
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Alias table cannot refer to itself");
// Disallow target is also an alias
auto target_storage = DatabaseCatalog::instance().tryGetTable(target_id, context_);
if (target_storage && target_storage->getName() == "Alias")
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Alias table cannot refer to another Alias table");
}
StoragePtr StorageAlias::getTargetTable(std::optional<TargetAccess> access_check) const
{
if (access_check)
{
if (access_check->column_names.empty())
access_check->context->checkAccess(access_check->access_type, target_database, target_table);
else
access_check->context->checkAccess(access_check->access_type, target_database, target_table, access_check->column_names);
}
return DatabaseCatalog::instance().getTable(StorageID(target_database, target_table), getContext());
}
bool StorageAlias::isTargetTableGranted(ContextPtr query_context, AccessType access_type, const String & column_name) const
{
if (!query_context)
return false;
auto access = query_context->getAccess();
if (column_name.empty())
return access->isGranted(access_type, target_database, target_table);
return access->isGranted(access_type, target_database, target_table, column_name);
}
/// AliasSink: Writes data to the target table using full INSERT pipeline
/// which triggers materialized views on the target table.
class AliasSink final : public SinkToStorage, WithContext
{
public:
AliasSink(
StorageAlias & storage_,
ContextPtr context_,
const StorageMetadataPtr & metadata_snapshot_,
bool async_insert_)
: SinkToStorage(std::make_shared<const Block>(metadata_snapshot_->getSampleBlock()))
, WithContext(context_)
, storage(storage_)
, non_materialized_header(metadata_snapshot_->getSampleBlockNonMaterialized())
, async_insert(async_insert_)
{
}
~AliasSink() override
{
/// On cancellation without an exception (e.g. timeout_overflow_mode='break') neither
/// onFinish() nor onException() runs, leaving the nested executor started but unfinished.
/// Cancel it so ~PushingPipelineExecutor's finished-or-unwinding invariant holds.
if (executor)
{
try
{
executor->cancel();
}
catch (...)
{
tryLogCurrentException("AliasSink");
}
}
}
String getName() const override { return "AliasSink"; }
void onStart() override
{
StoragePtr target = storage.getTargetTable();
std::unique_ptr<ASTInsertQuery> insert = std::make_unique<ASTInsertQuery>();
insert->table_id = target->getStorageID();
ASTPtr query_ptr(insert.release());
auto insert_context = Context::createCopy(getContext());
insert_context->makeQueryContext();
if (getContext()->hasQueryContext())
insert_context->setQueryAccessInfo(getContext()->getQueryContext()->getQueryAccessInfoPtr());
addInterpreterContext(insert_context);
/// This sink is one branch of the outer query's `max_insert_threads` fan-out (or its only
/// stream). Keep the nested INSERT single-stream: with the outer fan-out already in place,
/// letting every branch fan out again would multiply the number of real sink branches (part
/// writers, squashing and compression buffers) up to `max_insert_threads^2`, exceeding the
/// budget the user allowed for this INSERT.
insert_context->setSetting("max_insert_threads", 1);
/// Thread the outer async-insert flag into the nested target pipeline so INSERT through
/// Alias matches a direct insert: async batches select async dedup settings and skip the
/// strict block-limit squashing that would slice their multi-token DeduplicationInfo and
/// break its row/offset invariant.
InterpreterInsertQuery interpreter(
query_ptr,
insert_context,
/* allow_materialized */ false,
/* no_squash */ false,
/* no_destination */ false,
/* async_insert */ async_insert);
block_io = interpreter.execute();
executor = std::make_unique<PushingPipelineExecutor>(block_io.pipeline);
executor->start();
}
void consume(Chunk & chunk) override
{
if (chunk.getNumRows() == 0)
return;
auto block = getHeader().cloneWithColumns(chunk.getColumns());
Block non_materialized_block;
for (const auto & col : non_materialized_header)
non_materialized_block.insert(block.getByName(col.name));
Chunk non_materialized_chunk(non_materialized_block.getColumns(), non_materialized_block.rows());
non_materialized_chunk.setChunkInfos(chunk.getChunkInfos().clone());
/// The nested INSERT re-anchors the deduplication info to its own chunks (its squashing and
/// `AddDeduplicationInfoTransform` call `updateOriginalBlock`). When this sink is fed by a
/// dependent materialized view whose inner query changed the number of rows, those chunks
/// no longer match the rows the info's offsets describe, and computing a data hash after
/// that re-anchoring would read out of the block's bounds. Cache the hashes now, while the
/// info is still consistent.
if (auto deduplication_info = non_materialized_chunk.getChunkInfos().get<DeduplicationInfo>())
deduplication_info->cacheDataHashes(data_hash_cache);
executor->push(std::move(non_materialized_chunk));
}
void onFinish() override
{
executor->finish();
executor.reset();
block_io.onFinish();
block_io = {};
}
void onException(std::exception_ptr) override
{
if (executor)
executor->cancel();
executor.reset();
block_io.onException();
block_io = {};
}
private:
StorageAlias & storage;
Block non_materialized_header;
bool async_insert;
BlockIO block_io;
std::unique_ptr<PushingPipelineExecutor> executor;
/// Memoizes the deduplication data hashes across the sibling chunks of one source block, so a
/// row-count-changing view fanned out into many chunks does not re-hash the source per chunk.
DeduplicationInfo::DataHashCache data_hash_cache;
};
void StorageAlias::read(
QueryPlan & query_plan,
const Names & column_names,
const StorageSnapshotPtr & /*storage_snapshot*/,
SelectQueryInfo & query_info,
ContextPtr local_context,
QueryProcessingStage::Enum processed_stage,
size_t max_block_size,
size_t num_streams)
{
auto target_storage = getTargetTable(TargetAccess{local_context, AccessType::SELECT, column_names});
auto lock = target_storage->lockForShare(
local_context->getCurrentQueryId(),
local_context->getSettingsRef()[Setting::lock_acquire_timeout]);
auto target_metadata = target_storage->getInMemoryMetadataPtr(local_context, false);
auto target_snapshot = target_storage->getStorageSnapshot(target_metadata, local_context);
target_storage->read(
query_plan,
column_names,
target_snapshot,
query_info,
local_context,
processed_stage,
max_block_size,
num_streams);
query_plan.addStorageHolder(target_storage);
query_plan.addTableLock(std::move(lock));
}
SinkToStoragePtr StorageAlias::write(
const ASTPtr & /*query*/,
const StorageMetadataPtr & /*metadata_snapshot*/,
ContextPtr local_context,
bool async_insert)
{
auto target_storage = getTargetTable(TargetAccess{local_context, AccessType::INSERT});
auto target_metadata = target_storage->getInMemoryMetadataPtr(local_context, false);
/// Use AliasSink which executes full INSERT pipeline on target
/// Therefore it will trigger the MV on the target
return std::make_shared<AliasSink>(*this, local_context, target_metadata, async_insert);
}
void StorageAlias::alter(
const AlterCommands & params,
ContextPtr local_context,
AlterLockHolder & table_lock_holder)
{
auto target_storage = getTargetTable(TargetAccess{local_context, AccessType::ALTER});
/// ALTER through alias on a table in a Replicated database is not supported
/// when the alias and target are in different databases. This is because the
/// DDL worker path is bypassed and metadata changes won't be replicated to
/// other replicas in ZooKeeper. If both are in the same Replicated database,
/// the DDL worker handles the ALTER correctly.
auto target_storage_id = target_storage->getStorageID();
if (getStorageID().database_name != target_storage_id.database_name)
{
auto target_db = DatabaseCatalog::instance().tryGetDatabase(target_storage_id.database_name);
if (target_db && target_db->getEngineName() == "Replicated")
{
throw Exception(
ErrorCodes::NOT_IMPLEMENTED,
"ALTER through alias is not supported when the target table is in a different Replicated database. "
"Execute the ALTER directly on the target table: {}",
target_storage_id.getNameForLogs());
}
}
target_storage->alter(params, local_context, table_lock_holder);
}
void StorageAlias::truncate(
const ASTPtr & query,
const StorageMetadataPtr & /*metadata_snapshot*/,
ContextPtr local_context,
TableExclusiveLockHolder & table_lock_holder)
{
auto target_storage = getTargetTable(TargetAccess{local_context, AccessType::TRUNCATE});
auto target_metadata = target_storage->getInMemoryMetadataPtr(local_context, false);
target_storage->truncate(query, target_metadata, local_context, table_lock_holder);
}
bool StorageAlias::optimize(
const ASTPtr & query,
const StorageMetadataPtr & /*metadata_snapshot*/,
const ASTPtr & partition,
bool final,
bool deduplicate,
const Names & deduplicate_by_columns,
bool cleanup,
ContextPtr local_context)
{
auto target_storage = getTargetTable(TargetAccess{local_context, AccessType::OPTIMIZE});
auto target_metadata = target_storage->getInMemoryMetadataPtr(local_context, false);
return target_storage->optimize(query, target_metadata, partition, final, deduplicate,
deduplicate_by_columns, cleanup, local_context);
}
Pipe StorageAlias::alterPartition(
const StorageMetadataPtr &,
const PartitionCommands & commands,
ContextPtr local_context)
{
auto target_storage = getTargetTable(TargetAccess{local_context, AccessType::ALTER});
auto target_metadata = target_storage->getInMemoryMetadataPtr(local_context, false);
return target_storage->alterPartition(target_metadata, commands, local_context);
}
void StorageAlias::checkAlterPartitionIsPossible(
const PartitionCommands & commands,
const StorageMetadataPtr & /*metadata_snapshot*/,
const Settings & settings,
ContextPtr local_context) const
{
auto target_storage = getTargetTable();
auto target_metadata = target_storage->getInMemoryMetadataPtr(local_context, false);
target_storage->checkAlterPartitionIsPossible(commands, target_metadata, settings, local_context);
}
void StorageAlias::mutate(const MutationCommands & commands, ContextPtr local_context)
{
auto target_storage = getTargetTable(TargetAccess{local_context, AccessType::ALTER});
target_storage->mutate(commands, local_context);
}
QueryPipeline StorageAlias::updateLightweight(const MutationCommands & commands, ContextPtr local_context)
{
auto target_storage = getTargetTable(TargetAccess{local_context, AccessType::ALTER});
auto lock = target_storage->lockForShare(
local_context->getCurrentQueryId(),
local_context->getSettingsRef()[Setting::lock_acquire_timeout]);
auto pipeline = target_storage->updateLightweight(commands, local_context);
/// The caller locks the alias, not the target, so the target needs its own share lock held
/// until the pipeline has committed the patch part.
QueryPlanResourceHolder target_resources;
target_resources.storage_holders.emplace_back(target_storage);
target_resources.table_locks.emplace_back(std::move(lock));
pipeline.addResources(std::move(target_resources));
return pipeline;
}
CancellationCode StorageAlias::killMutation(const String & mutation_id)
{
return getTargetTable()->killMutation(mutation_id);
}
void StorageAlias::waitForMutation(const String & mutation_id, bool wait_for_another_mutation)
{
getTargetTable()->waitForMutation(mutation_id, wait_for_another_mutation);
}
void StorageAlias::setMutationCSN(const String & mutation_id, UInt64 csn)
{
getTargetTable()->setMutationCSN(mutation_id, csn);
}
namespace
{
/// Holds the resolved target StoragePtr alongside its task list
struct AliasCheckTasks : IStorage::DataValidationTasksBase
{
StoragePtr target;
IStorage::DataValidationTasksPtr inner;
AliasCheckTasks(StoragePtr target_, IStorage::DataValidationTasksPtr inner_)
: target(std::move(target_)), inner(std::move(inner_))
{
chassert(inner);
}
size_t size() const override { return inner->size(); }
};
}
IStorage::DataValidationTasksPtr StorageAlias::getCheckTaskList(const CheckTaskFilter & filter, ContextPtr query_context)
{
auto target = getTargetTable(TargetAccess{query_context, AccessType::CHECK});
auto inner = target->getCheckTaskList(filter, query_context);
return std::make_shared<AliasCheckTasks>(std::move(target), std::move(inner));
}
std::optional<CheckResult> StorageAlias::checkDataNext(DataValidationTasksPtr & check_task_list)
{
auto * tasks = assert_cast<AliasCheckTasks *>(check_task_list.get());
return tasks->target->checkDataNext(tasks->inner);
}
CancellationCode StorageAlias::killPartMoveToShard(const UUID & task_uuid)
{
return getTargetTable()->killPartMoveToShard(task_uuid);
}
void StorageAlias::updateExternalDynamicMetadataIfExists(ContextPtr local_context)
{
getTargetTable()->updateExternalDynamicMetadataIfExists(local_context);
}
std::optional<QueryPipeline> StorageAlias::distributedWrite(const ASTInsertQuery & query, ContextPtr local_context)
{
return getTargetTable(TargetAccess{local_context, AccessType::INSERT})->distributedWrite(query, local_context);
}
StorageSnapshotPtr StorageAlias::getStorageSnapshot(const StorageMetadataPtr & metadata_snapshot, ContextPtr query_context) const
{
return getTargetTable()->getStorageSnapshot(metadata_snapshot, query_context);
}
StorageSnapshotPtr StorageAlias::getStorageSnapshotWithoutData(const StorageMetadataPtr & metadata_snapshot, ContextPtr query_context) const
{
return getTargetTable()->getStorageSnapshotWithoutData(metadata_snapshot, query_context);
}
bool StorageAlias::supportsTrivialCountOptimization(const StorageSnapshotPtr & storage_snapshot, ContextPtr query_context) const
{
if (!storage_snapshot)
return false;
bool has_select_access = false;
for (const auto & column : storage_snapshot->metadata->getColumns())
{
if (isTargetTableGranted(query_context, AccessType::SELECT, column.name))
{
has_select_access = true;
break;
}
}
if (!has_select_access)
return false;
auto target = tryGetTargetTable();
return target && target->supportsTrivialCountOptimization(storage_snapshot, query_context);
}
std::optional<UInt64> StorageAlias::totalRows(ContextPtr query_context) const
{
if (!isTargetTableGranted(query_context, AccessType::SHOW_TABLES, {}))
return {};
auto target = tryGetTargetTable();
return target ? target->totalRows(query_context) : std::optional<UInt64>{};
}
std::optional<UInt64> StorageAlias::totalBytes(ContextPtr query_context) const
{
if (!isTargetTableGranted(query_context, AccessType::SHOW_TABLES, {}))
return {};
auto target = tryGetTargetTable();
return target ? target->totalBytes(query_context) : std::optional<UInt64>{};
}
void StorageAlias::rename(const String & /* new_path_to_table_data */, const StorageID & new_table_id)
{
// Only rename the alias itself, not the target table
renameInMemory(new_table_id);
}
QueryProcessingStage::Enum StorageAlias::getQueryProcessingStage(
ContextPtr local_context,
QueryProcessingStage::Enum to_stage,
const StorageSnapshotPtr & /* storage_snapshot */,
SelectQueryInfo & query_info) const
{
auto target_storage = getTargetTable();
auto target_metadata = target_storage->getInMemoryMetadataPtr(local_context, false);
auto target_snapshot = target_storage->getStorageSnapshot(target_metadata, local_context);
return target_storage->getQueryProcessingStage(local_context, to_stage, target_snapshot, query_info);
}
void registerStorageAlias(StorageFactory & factory);
void registerStorageAlias(StorageFactory & factory)
{
factory.registerStorage("Alias", [](const StorageFactory::Arguments & args)
{
// Supported syntaxes:
// CREATE TABLE t2 ENGINE = Alias(t)
// CREATE TABLE t2 ENGINE = Alias(db, t)
// CREATE TABLE t2 ENGINE = Alias('t')
// CREATE TABLE t2 ENGINE = Alias('db', 't')
auto local_context = args.getLocalContext();
String target_database;
String target_table;
if (args.engine_args.empty())
{
// Note: CREATE TABLE ... AS ... syntax is not supported
throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH,
"Storage Alias requires target table name in ENGINE arguments. "
"Use: ENGINE = Alias('table_name') or ENGINE = Alias('database', 'table_name').");
}
else if (args.engine_args.size() == 1)
{
// Syntax: ENGINE = Alias(table_name) or ENGINE = Alias(db.table_name)
args.engine_args[0] = evaluateConstantExpressionOrIdentifierAsLiteral(args.engine_args[0], local_context);
String table_arg = checkAndGetLiteralArgument<String>(args.engine_args[0], "table_name");
auto dot_pos = table_arg.find('.');
if (dot_pos != String::npos)
{
target_database = table_arg.substr(0, dot_pos);
target_table = table_arg.substr(dot_pos + 1);
}
else
{
target_table = table_arg;
target_database = args.table_id.database_name;
}
}
else if (args.engine_args.size() == 2)
{
// Syntax: ENGINE = Alias(database_name, table_name)
args.engine_args[0] = evaluateConstantExpressionOrIdentifierAsLiteral(args.engine_args[0], local_context);
args.engine_args[1] = evaluateConstantExpressionOrIdentifierAsLiteral(args.engine_args[1], local_context);
target_database = checkAndGetLiteralArgument<String>(args.engine_args[0], "database_name");
target_table = checkAndGetLiteralArgument<String>(args.engine_args[1], "table_name");
}
else
{
throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH,
"Storage Alias requires at most 2 arguments: database name and table name");
}
// Storage Alias does not support explicit column definitions
// Columns are always dynamically fetched from the target table
// Only check for CREATE, not for ATTACH/RESTORE
if (!args.columns.empty() && args.mode == LoadingStrictnessLevel::CREATE)
{
throw Exception(ErrorCodes::BAD_ARGUMENTS,
"Storage Alias does not support explicit column definitions");
}
if (!(isLoadingFromExistingMetadata(args.mode) || args.query.attach_short_syntax))
local_context->checkAccess(AccessType::SHOW_COLUMNS, target_database, target_table);
return std::make_shared<StorageAlias>(
args.table_id,
local_context,
target_database,
target_table);
},
{
.supports_schema_inference = true
},
Documentation{
.description = R"DOCS_MD(
# Alias table engine
The `Alias` engine creates a proxy to another table. All read and write operations are forwarded to the target table, while the alias itself stores no data and only maintains a reference to the target table.
## Creating a Table {#creating-a-table}
```sql
CREATE TABLE [db_name.]alias_name
ENGINE = Alias(target_table)
```
Or with explicit database name:
```sql
CREATE TABLE [db_name.]alias_name
ENGINE = Alias(target_db, target_table)
```
:::note
The `Alias` table does not support explicit column definitions. Columns are automatically inherited from the target table. This ensures that the alias always matches the target table's schema.
:::
## Engine Parameters {#engine-parameters}
- **`target_db (optional)`** — Name of the database containing the target table.
- **`target_table`** — Name of the target table.
:::note
When `target_db` is omitted and `target_table` is not fully qualified (e.g., `Alias('my_table')`), the target is resolved to the same database as the alias itself, not the session's current database.
:::
## Supported Operations {#supported-operations}
The `Alias` table engine supports all major operations.
### Operations on Target Table {#operations-on-target}
These operations are proxied to the target table:
| Operation | Support | Description |
|-----------|---------|-------------|
| `SELECT` | ✅ | Read data from target table |
| `INSERT` | ✅ | Write data to target table |
| `INSERT SELECT` | ✅ | Batch insert into target table |
| `ALTER TABLE ADD COLUMN` | ✅ | Add columns to target table |
| `ALTER TABLE MODIFY SETTING` | ✅ | Modify target table settings |
| `ALTER TABLE PARTITION` | ✅ | Partition operations (DETACH/ATTACH/DROP) on target |
| `ALTER TABLE UPDATE` | ✅ | Update rows in target table (mutation) |
| `ALTER TABLE DELETE` | ✅ | Delete rows from target table (mutation) |
| `OPTIMIZE TABLE` | ✅ | Optimize target table (merge parts) |
| `TRUNCATE TABLE` | ✅ | Truncate target table |
### Operations on Alias Itself {#operations-on-alias}
These operations only affect the alias, **not** the target table:
| Operation | Support | Description |
|-----------|---------|-------------|
| `DROP TABLE` | ✅ | Drop the alias only, target table remains unchanged |
| `RENAME TABLE` | ✅ | Rename the alias only, target table remains unchanged |
## Usage Examples {#usage-examples}
### Basic Alias Creation {#basic-alias-creation}
Create a simple alias in the same database:
```sql
-- Create source table
CREATE TABLE source_data (
id UInt32,
name String,
value Float64
) ENGINE = MergeTree
ORDER BY id;
-- Insert some data
INSERT INTO source_data VALUES (1, 'one', 10.1), (2, 'two', 20.2);
-- Create alias
CREATE TABLE data_alias ENGINE = Alias('source_data');
-- Query through alias
SELECT * FROM data_alias;
```
```text
┌─id─┬─name─┬─value─┐
│ 1 │ one │ 10.1 │
│ 2 │ two │ 20.2 │
└────┴──────┴───────┘
```
### Cross-Database Alias {#cross-database-alias}
Create an alias pointing to a table in a different database:
```sql
-- Create databases
CREATE DATABASE db1;
CREATE DATABASE db2;
-- Create source table in db1
CREATE TABLE db1.events (
timestamp DateTime,
event_type String,
user_id UInt32
) ENGINE = MergeTree
ORDER BY timestamp;
-- Create alias in db2 pointing to db1.events
CREATE TABLE db2.events_alias ENGINE = Alias('db1', 'events');
-- Or using database.table format
CREATE TABLE db2.events_alias2 ENGINE = Alias('db1.events');
-- Both aliases work identically
INSERT INTO db2.events_alias VALUES (now(), 'click', 100);
SELECT * FROM db2.events_alias2;
```
### Write Operations Through Alias {#write-operations}
All write operations are forwarded to the target table:
```sql
CREATE TABLE metrics (
ts DateTime,
metric_name String,
value Float64
) ENGINE = MergeTree
ORDER BY ts;
CREATE TABLE metrics_alias ENGINE = Alias('metrics');
-- Insert through alias
INSERT INTO metrics_alias VALUES
(now(), 'cpu_usage', 45.2),
(now(), 'memory_usage', 78.5);
-- Insert with SELECT
INSERT INTO metrics_alias
SELECT now(), 'disk_usage', number * 10
FROM system.numbers
LIMIT 5;
-- Verify data is in the target table
SELECT count() FROM metrics; -- Returns 7
SELECT count() FROM metrics_alias; -- Returns 7
```
### Schema Modification {#schema-modification}
Alter operations modify the target table schema:
```sql
CREATE TABLE users (
id UInt32,
name String
) ENGINE = MergeTree
ORDER BY id;
CREATE TABLE users_alias ENGINE = Alias('users');
-- Add column through alias
ALTER TABLE users_alias ADD COLUMN email String DEFAULT '';
-- Column is added to target table
DESCRIBE users;
```
```text
┌─name──┬─type───┬─default_type─┬─default_expression─┐
│ id │ UInt32 │ │ │
│ name │ String │ │ │
│ email │ String │ DEFAULT │ '' │
└───────┴────────┴──────────────┴────────────────────┘
```
### Data Mutations {#data-mutations}
UPDATE and DELETE operations are supported:
```sql
CREATE TABLE products (
id UInt32,
name String,
price Float64,
status String DEFAULT 'active'
) ENGINE = MergeTree
ORDER BY id;
CREATE TABLE products_alias ENGINE = Alias('products');
INSERT INTO products_alias VALUES
(1, 'item_one', 100.0, 'active'),
(2, 'item_two', 200.0, 'active'),
(3, 'item_three', 300.0, 'inactive');
-- Update through alias
ALTER TABLE products_alias UPDATE price = price * 1.1 WHERE status = 'active';
-- Delete through alias
ALTER TABLE products_alias DELETE WHERE status = 'inactive';
-- Changes are applied to target table
SELECT * FROM products ORDER BY id;
```
```text
┌─id─┬─name─────┬─price─┬─status─┐
│ 1 │ item_one │ 110.0 │ active │
│ 2 │ item_two │ 220.0 │ active │
└────┴──────────┴───────┴────────┘
```
### Partition Operations {#partition-operations}
For partitioned tables, partition operations are forwarded:
```sql
CREATE TABLE logs (
date Date,
level String,
message String
) ENGINE = MergeTree
PARTITION BY toYYYYMM(date)
ORDER BY date;
CREATE TABLE logs_alias ENGINE = Alias('logs');
INSERT INTO logs_alias VALUES
('2024-01-15', 'INFO', 'message1'),
('2024-02-15', 'ERROR', 'message2'),
('2024-03-15', 'INFO', 'message3');
-- Detach partition through alias
ALTER TABLE logs_alias DETACH PARTITION '202402';
SELECT count() FROM logs_alias; -- Returns 2 (partition 202402 detached)
-- Attach partition back
ALTER TABLE logs_alias ATTACH PARTITION '202402';
SELECT count() FROM logs_alias; -- Returns 3
```
### Table Optimization {#table-optimization}
Optimize operations merge parts in the target table:
```sql
CREATE TABLE events (
id UInt32,
data String
) ENGINE = MergeTree
ORDER BY id;
CREATE TABLE events_alias ENGINE = Alias('events');
-- Multiple inserts create multiple parts
INSERT INTO events_alias VALUES (1, 'data1');
INSERT INTO events_alias VALUES (2, 'data2');
INSERT INTO events_alias VALUES (3, 'data3');
-- Check parts count
SELECT count() FROM system.parts
WHERE database = currentDatabase()
AND table = 'events'
AND active;
-- Optimize through alias
OPTIMIZE TABLE events_alias FINAL;
-- Parts are merged in target table
SELECT count() FROM system.parts
WHERE database = currentDatabase()
AND table = 'events'
AND active; -- Returns 1
```
### Alias Management {#alias-management}
Aliases can be renamed or dropped independently:
```sql
CREATE TABLE important_data (
id UInt32,
value String
) ENGINE = MergeTree
ORDER BY id;
INSERT INTO important_data VALUES (1, 'critical'), (2, 'important');
CREATE TABLE old_alias ENGINE = Alias('important_data');
-- Rename alias (target table unchanged)
RENAME TABLE old_alias TO new_alias;
-- Create another alias to same table
CREATE TABLE another_alias ENGINE = Alias('important_data');
-- Drop one alias (target table and other aliases unchanged)
DROP TABLE new_alias;
SELECT * FROM another_alias; -- Still works
SELECT count() FROM important_data; -- Data intact, returns 2
```
)DOCS_MD",
.syntax = "ENGINE = Alias([target_db, ]target_table)"});
}
}