-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Expand file tree
/
Copy pathContext.h
More file actions
1879 lines (1489 loc) · 85 KB
/
Context.h
File metadata and controls
1879 lines (1489 loc) · 85 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
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#pragma once
#include <base/types.h>
#include <Core/Block_fwd.h>
#include <Common/Exception.h>
#include <Common/MultiVersion.h>
#include <Common/ThreadPool_fwd.h>
#include <Common/IThrottler.h>
#include <Common/SettingSource.h>
#include <Common/SharedMutex.h>
#include <Common/SharedMutexHelper.h>
#include <Common/StopToken.h>
#include <Core/UUID.h>
#include <IO/ReadSettings.h>
#include <IO/WriteSettings.h>
#include <Disks/IO/getThreadPoolReader.h>
#include <Formats/FormatSettings.h>
#include <Interpreters/ClientInfo.h>
#include <Interpreters/Context_fwd.h>
#include <Interpreters/StorageID.h>
#include <Interpreters/MergeTreeTransactionHolder.h>
#include <Parsers/IAST_fwd.h>
#include <Server/HTTP/HTTPContext.h>
#include <Storages/IStorage_fwd.h>
#include <Backups/BackupsInMemoryHolder.h>
#include <Poco/AutoPtr.h>
#include "config.h"
#include <functional>
#include <memory>
#include <mutex>
#include <optional>
namespace Poco::Net
{
class IPAddress;
class SocketAddress;
}
namespace Poco::Util
{
class AbstractConfiguration;
}
namespace zkutil
{
class ZooKeeper;
using ZooKeeperPtr = std::shared_ptr<ZooKeeper>;
}
namespace Coordination
{
struct Request;
using RequestPtr = std::shared_ptr<Request>;
using Requests = std::vector<RequestPtr>;
}
struct OvercommitTracker;
namespace DB
{
class ASTSelectQuery;
class SystemLogs;
class ColumnsDescription;
struct ContextSharedPart;
class ContextAccess;
class ContextAccessWrapper;
class Field;
struct User;
using UserPtr = std::shared_ptr<const User>;
struct SettingsProfilesInfo;
struct EnabledRolesInfo;
struct RowPolicyFilter;
using RowPolicyFilterPtr = std::shared_ptr<const RowPolicyFilter>;
class EnabledQuota;
struct QuotaUsage;
class AccessFlags;
struct AccessRightsElement;
class AccessRightsElements;
enum class RowPolicyFilterType : uint8_t;
struct RolesOrUsersSet;
class EmbeddedDictionaries;
class ExternalDictionariesLoader;
class ExternalUserDefinedExecutableFunctionsLoader;
class IUserDefinedSQLObjectsStorage;
class IWorkloadEntityStorage;
class InterserverCredentials;
using InterserverCredentialsPtr = std::shared_ptr<const InterserverCredentials>;
class InterserverIOHandler;
class AsynchronousMetrics;
class BackgroundSchedulePool;
class MergeList;
class MovesList;
class ReplicatedFetchList;
class RefreshSet;
class Cluster;
class Compiler;
class MarkCache;
class PrimaryIndexCache;
class PageCache;
class MMappedFileCache;
class UncompressedCache;
class IcebergMetadataFilesCache;
class ParquetMetadataCache;
class VectorSimilarityIndexCache;
class TextIndexTokensCache;
class TextIndexHeaderCache;
class TextIndexPostingsCache;
class ProcessList;
class QueryStatus;
using QueryStatusPtr = std::shared_ptr<QueryStatus>;
class Macros;
struct Progress;
struct FileProgress;
class Clusters;
class QueryResultCache;
class QueryConditionCache;
class ISystemLog;
class QueryLog;
class QueryMetricLog;
class QueryThreadLog;
class QueryViewsLog;
class PartLog;
class BackgroundSchedulePoolLog;
class TextLog;
class TraceLog;
class MetricLog;
class TransposedMetricLog;
class AsynchronousMetricLog;
class OpenTelemetrySpanLog;
class ZooKeeperLog;
class ZooKeeperConnectionLog;
class AggregatedZooKeeperLog;
class IcebergMetadataLog;
class DeltaMetadataLog;
class SessionLog;
class BackupsWorker;
class TransactionsInfoLog;
class ProcessorsProfileLog;
class FilesystemCacheLog;
class FilesystemReadPrefetchesLog;
class ObjectStorageQueueLog;
class AsynchronousInsertLog;
class BackupLog;
class BlobStorageLog;
class DeadLetterQueue;
class IAsynchronousReader;
class IOUringReader;
struct MergeTreeSettings;
struct DatabaseReplicatedSettings;
struct DistributedSettings;
struct InitialAllRangesAnnouncement;
struct ParallelReadRequest;
struct ParallelReadResponse;
class S3SettingsByEndpoint;
class AzureSettingsByEndpoint;
class IDatabase;
class DDLWorker;
class ITableFunction;
using TableFunctionPtr = std::shared_ptr<ITableFunction>;
class Block;
class ActionLocksManager;
using ActionLocksManagerPtr = std::shared_ptr<ActionLocksManager>;
class ShellCommand;
class ICompressionCodec;
class AccessControl;
class GSSAcceptorContext;
struct Settings;
struct SettingChange;
class SettingsChanges;
struct SettingsConstraintsAndProfileIDs;
struct AlterSettingsProfileElements;
class RemoteHostFilter;
class IDisk;
using DiskPtr = std::shared_ptr<IDisk>;
class DiskSelector;
using DiskSelectorPtr = std::shared_ptr<const DiskSelector>;
using DisksMap = std::map<String, DiskPtr, std::less<>>;
class IStoragePolicy;
using StoragePolicyPtr = std::shared_ptr<const IStoragePolicy>;
using StoragePoliciesMap = std::map<String, StoragePolicyPtr>;
class StoragePolicySelector;
using StoragePolicySelectorPtr = std::shared_ptr<const StoragePolicySelector>;
class ServerType;
template <class Queue>
class MergeTreeBackgroundExecutor;
class AsyncLoader;
class HTTPHeaderFilter;
struct AsyncReadCounters;
struct ICgroupsReader;
class WasmModuleManager;
struct TemporaryTableHolder;
using TemporaryTablesMapping = std::map<String, std::shared_ptr<TemporaryTableHolder>>;
using ClusterPtr = std::shared_ptr<Cluster>;
class LoadTask;
using LoadTaskPtr = std::shared_ptr<LoadTask>;
using LoadTaskPtrs = std::vector<LoadTaskPtr>;
class IClassifier;
using ClassifierPtr = std::shared_ptr<IClassifier>;
class IResourceManager;
using ResourceManagerPtr = std::shared_ptr<IResourceManager>;
/// Scheduling policy can be changed using `background_merges_mutations_scheduling_policy` config option.
/// By default concurrent merges are scheduled using "round_robin" to ensure fair and starvation-free operation.
/// Previously in heavily overloaded shards big merges could possibly be starved by smaller
/// merges due to the use of strict priority scheduling "shortest_task_first".
class DynamicRuntimeQueue;
using MergeMutateBackgroundExecutor = MergeTreeBackgroundExecutor<DynamicRuntimeQueue>;
using MergeMutateBackgroundExecutorPtr = std::shared_ptr<MergeMutateBackgroundExecutor>;
class RoundRobinRuntimeQueue;
using OrdinaryBackgroundExecutor = MergeTreeBackgroundExecutor<RoundRobinRuntimeQueue>;
using OrdinaryBackgroundExecutorPtr = std::shared_ptr<OrdinaryBackgroundExecutor>;
struct PartUUIDs;
using PartUUIDsPtr = std::shared_ptr<PartUUIDs>;
class KeeperDispatcher;
struct WriteSettings;
class IInputFormat;
class IOutputFormat;
using InputFormatPtr = std::shared_ptr<IInputFormat>;
using OutputFormatPtr = std::shared_ptr<IOutputFormat>;
class IVolume;
using VolumePtr = std::shared_ptr<IVolume>;
struct NamedSession;
struct BackgroundTaskSchedulingSettings;
#if USE_NLP
class SynonymsExtensions;
class Lemmatizers;
#endif
class ZooKeeperMetadataTransaction;
using ZooKeeperMetadataTransactionPtr = std::shared_ptr<ZooKeeperMetadataTransaction>;
class AsynchronousInsertQueue;
/// Callback for external tables initializer
using ExternalTablesInitializer = std::function<void(ContextPtr)>;
/// Callback for initialize input()
using InputInitializer = std::function<void(ContextPtr, const StoragePtr &)>;
/// Callback for reading blocks of data from client for function input()
using InputBlocksReader = std::function<Block(ContextPtr)>;
/// Used in distributed task processing
struct ClusterFunctionReadTaskResponse;
using ClusterFunctionReadTaskResponsePtr = std::shared_ptr<ClusterFunctionReadTaskResponse>;
using ClusterFunctionReadTaskCallback = std::function<ClusterFunctionReadTaskResponsePtr()>;
using MergeTreeAllRangesCallback = std::function<void(InitialAllRangesAnnouncement)>;
using MergeTreeReadTaskCallback = std::function<std::optional<ParallelReadResponse>(ParallelReadRequest)>;
using BlockMarshallingCallback = std::function<Block(const Block & block)>;
class RuntimeDataflowStatisticsCacheUpdater;
using RuntimeDataflowStatisticsCacheUpdaterPtr = std::shared_ptr<RuntimeDataflowStatisticsCacheUpdater>;
struct QueryPlanAndSets;
using QueryPlanDeserializationCallback = std::function<std::shared_ptr<QueryPlanAndSets>()>;
class TemporaryDataOnDiskScope;
using TemporaryDataOnDiskScopePtr = std::shared_ptr<TemporaryDataOnDiskScope>;
class PreparedSetsCache;
using PreparedSetsCachePtr = std::shared_ptr<PreparedSetsCache>;
class ReverseLookupCache;
using ReverseLookupCachePtr = std::shared_ptr<ReverseLookupCache>;
class ContextTimeSeriesTagsCollector;
using PartitionIdToMaxBlock = std::unordered_map<String, Int64>;
using PartitionIdToMaxBlockPtr = std::shared_ptr<const PartitionIdToMaxBlock>;
class SessionTracker;
struct ServerSettings;
struct StorageInMemoryMetadata;
using StorageMetadataPtr = std::shared_ptr<const StorageInMemoryMetadata>;
struct StorageSnapshot;
using StorageSnapshotPtr = std::shared_ptr<StorageSnapshot>;
class SystemAllocatedMemoryHolder;
using SystemAllocatedMemoryHolderPtr = std::shared_ptr<SystemAllocatedMemoryHolder>;
/// IRuntimeFilterLookup allows to store and find per-query runtime filters under unique names.
/// Runtime filters are used to optimize JOINs in some cases by building a bloom filter from the right side
/// of the JOIN and use it to do early pre-filtering on the left side of the JOIN.
struct IRuntimeFilterLookup;
using RuntimeFilterLookupPtr = std::shared_ptr<IRuntimeFilterLookup>;
RuntimeFilterLookupPtr createRuntimeFilterLookup();
class QueryMetadataCache;
using QueryMetadataCachePtr = std::shared_ptr<QueryMetadataCache>;
using QueryMetadataCacheWeakPtr = std::weak_ptr<QueryMetadataCache>;
using DatabasePtr = std::shared_ptr<IDatabase>;
using DatabaseAndTable = std::pair<DatabasePtr, StoragePtr>;
/// An empty interface for an arbitrary object that may be attached by a shared pointer
/// to query context, when using ClickHouse as a library.
struct IHostContext
{
virtual ~IHostContext() = default;
};
using IHostContextPtr = std::shared_ptr<IHostContext>;
/// A small class which owns ContextShared.
/// We don't use something like unique_ptr directly to allow ContextShared type to be incomplete.
struct SharedContextHolder
{
~SharedContextHolder();
SharedContextHolder();
explicit SharedContextHolder(std::unique_ptr<ContextSharedPart> shared_context);
SharedContextHolder(SharedContextHolder &&) noexcept;
SharedContextHolder & operator=(SharedContextHolder &&) noexcept;
ContextSharedPart * get() const { return shared.get(); }
void reset();
private:
std::unique_ptr<ContextSharedPart> shared;
};
class ContextSharedMutex : public SharedMutexHelper<ContextSharedMutex>
{
private:
using Base = SharedMutexHelper<ContextSharedMutex, SharedMutex>;
friend class SharedMutexHelper<ContextSharedMutex, SharedMutex>;
void lockImpl();
void lockSharedImpl();
};
class ContextData
{
protected:
ContextSharedPart * shared;
ClientInfo client_info;
ExternalTablesInitializer external_tables_initializer_callback;
QueryPlanDeserializationCallback query_plan_deserialization_callback;
InputInitializer input_initializer_callback;
InputBlocksReader input_blocks_reader;
std::optional<UUID> user_id;
std::shared_ptr<std::vector<UUID>> current_roles;
std::shared_ptr<std::vector<UUID>> external_roles;
std::shared_ptr<const SettingsConstraintsAndProfileIDs> settings_constraints_and_current_profiles;
mutable std::shared_ptr<const ContextAccess> access;
mutable bool need_recalculate_access = true;
String current_database;
std::unique_ptr<Settings> settings{}; /// Setting for query execution.
using ProgressCallback = std::function<void(const Progress & progress)>;
ProgressCallback progress_callback; /// Callback for tracking progress of query execution.
using FileProgressCallback = std::function<void(const FileProgress & progress)>;
FileProgressCallback file_progress_callback; /// Callback for tracking progress of file loading.
using InteractiveCancelCallback = std::function<bool()>;
InteractiveCancelCallback interactive_cancel_callback; /// Callback for usage in interactive sessions with CompletedPipelineExecutor
std::weak_ptr<QueryStatus> process_list_elem; /// For tracking total resource usage for query.
bool has_process_list_elem = false; /// It's impossible to check if weak_ptr was initialized or not
struct InsertionTableInfo
{
StorageID table = StorageID::createEmpty();
std::optional<Names> column_names;
std::shared_ptr<ColumnsDescription> columns_description;
};
InsertionTableInfo insertion_table_info; /// Saved information about insertion table in query context
bool is_distributed = false; /// Whether the current context it used for distributed query
String default_format; /// Format, used when server formats data by itself and if query does not have FORMAT specification.
/// Thus, used in HTTP interface. If not specified - then some globally default format is used.
String insert_format; /// Format, used in insert query.
TemporaryTablesMapping external_tables_mapping;
/// Query scalars
Scalars scalars;
/// Used to store constant values which are different on each instance during distributed plan, such as _shard_num.
Scalars special_scalars;
/// Mapping between identifiers and time series tags collected in the context of the currently executed query.
std::shared_ptr<ContextTimeSeriesTagsCollector> time_series_tags_collector;
/// Used in s3Cluster table function. With this callback, a worker node could ask an initiator
/// about next file to read from s3.
std::optional<ClusterFunctionReadTaskCallback> next_task_callback;
/// Used in parallel reading from replicas. A replica tells about its intentions to read
/// some ranges from some part and initiator will tell the replica about whether it is accepted or denied.
std::optional<MergeTreeReadTaskCallback> merge_tree_read_task_callback;
std::optional<MergeTreeAllRangesCallback> merge_tree_all_ranges_callback;
UUID parallel_replicas_group_uuid{UUIDHelpers::Nil};
BlockMarshallingCallback block_marshalling_callback;
mutable RuntimeDataflowStatisticsCacheUpdaterPtr dataflow_cache_updater;
bool is_under_restore = false;
/// This parameter can be set by the HTTP client to tune the behavior of output formats for compatibility.
UInt64 client_protocol_version = 0;
/// Max block numbers in partitions to read from MergeTree tables.
/// Saved separately for each table uuid used in the query.
std::unordered_map<UUID, PartitionIdToMaxBlockPtr> partition_id_to_max_block;
public:
/// Record entities accessed by current query, and store this information in system.query_log.
struct QueryAccessInfo
{
QueryAccessInfo() = default;
QueryAccessInfo(const QueryAccessInfo & rhs)
{
std::lock_guard<std::mutex> lock(rhs.mutex);
databases = rhs.databases;
tables = rhs.tables;
columns = rhs.columns;
partitions = rhs.partitions;
projections = rhs.projections;
views = rhs.views;
row_policies = rhs.row_policies;
}
QueryAccessInfo(QueryAccessInfo && rhs) = delete;
QueryAccessInfo & operator=(QueryAccessInfo rhs)
{
swap(rhs);
return *this;
}
void swap(QueryAccessInfo & rhs) noexcept TSA_NO_THREAD_SAFETY_ANALYSIS
{
/// TSA_NO_THREAD_SAFETY_ANALYSIS because it doesn't support scoped_lock
std::scoped_lock lck{mutex, rhs.mutex};
std::swap(databases, rhs.databases);
std::swap(tables, rhs.tables);
std::swap(columns, rhs.columns);
std::swap(partitions, rhs.partitions);
std::swap(projections, rhs.projections);
std::swap(views, rhs.views);
std::swap(row_policies, rhs.row_policies);
}
/// To prevent a race between copy-constructor and other uses of this structure.
mutable std::mutex mutex{};
std::set<std::string> databases TSA_GUARDED_BY(mutex){};
std::set<std::string> tables TSA_GUARDED_BY(mutex){};
std::set<std::string> columns TSA_GUARDED_BY(mutex){};
std::set<std::string> partitions TSA_GUARDED_BY(mutex){};
std::set<std::string> projections TSA_GUARDED_BY(mutex){};
std::set<std::string> views TSA_GUARDED_BY(mutex){};
std::set<std::string> row_policies TSA_GUARDED_BY(mutex){};
};
using QueryAccessInfoPtr = std::shared_ptr<QueryAccessInfo>;
protected:
/// In some situations, we want to be able to transfer the access info from children back to parents (e.g. definers context).
/// Therefore, query_access_info must be a pointer.
QueryAccessInfoPtr query_access_info;
public:
/// Record names of created objects of factories (for testing, etc)
struct QueryFactoriesInfo
{
QueryFactoriesInfo() = default;
QueryFactoriesInfo(const QueryFactoriesInfo & rhs)
{
std::lock_guard<std::mutex> lock(rhs.mutex);
aggregate_functions = rhs.aggregate_functions;
aggregate_function_combinators = rhs.aggregate_function_combinators;
database_engines = rhs.database_engines;
data_type_families = rhs.data_type_families;
dictionaries = rhs.dictionaries;
formats = rhs.formats;
functions = rhs.functions;
storages = rhs.storages;
table_functions = rhs.table_functions;
executable_user_defined_functions = rhs.executable_user_defined_functions;
sql_user_defined_functions = rhs.sql_user_defined_functions;
}
QueryFactoriesInfo(QueryFactoriesInfo && rhs) = delete;
std::unordered_set<std::string> aggregate_functions TSA_GUARDED_BY(mutex);
std::unordered_set<std::string> aggregate_function_combinators TSA_GUARDED_BY(mutex);
std::unordered_set<std::string> database_engines TSA_GUARDED_BY(mutex);
std::unordered_set<std::string> data_type_families TSA_GUARDED_BY(mutex);
std::unordered_set<std::string> dictionaries TSA_GUARDED_BY(mutex);
std::unordered_set<std::string> formats TSA_GUARDED_BY(mutex);
std::unordered_set<std::string> functions TSA_GUARDED_BY(mutex);
std::unordered_set<std::string> storages TSA_GUARDED_BY(mutex);
std::unordered_set<std::string> table_functions TSA_GUARDED_BY(mutex);
std::unordered_set<std::string> executable_user_defined_functions TSA_GUARDED_BY(mutex);
std::unordered_set<std::string> sql_user_defined_functions TSA_GUARDED_BY(mutex);
mutable std::mutex mutex;
};
struct QueryPrivilegesInfo
{
QueryPrivilegesInfo() = default;
QueryPrivilegesInfo(const QueryPrivilegesInfo & rhs)
{
std::lock_guard<std::mutex> lock(rhs.mutex);
used_privileges = rhs.used_privileges;
missing_privileges = rhs.missing_privileges;
}
QueryPrivilegesInfo(QueryPrivilegesInfo && rhs) = delete;
std::unordered_set<std::string> used_privileges TSA_GUARDED_BY(mutex);
std::unordered_set<std::string> missing_privileges TSA_GUARDED_BY(mutex);
mutable std::mutex mutex;
};
using QueryPrivilegesInfoPtr = std::shared_ptr<QueryPrivilegesInfo>;
protected:
/// Needs to be changed while having const context in factories methods
mutable QueryFactoriesInfo query_factories_info;
QueryPrivilegesInfoPtr query_privileges_info;
/// Query metrics for reading data asynchronously with IAsynchronousReader.
mutable std::shared_ptr<AsyncReadCounters> async_read_counters;
/// TODO: maybe replace with temporary tables?
StoragePtr view_source; /// Temporary StorageValues used to generate alias columns for materialized views
Tables table_function_results; /// Temporary tables obtained by execution of table functions. Keyed by AST tree id.
mutable std::mutex table_function_results_mutex;
ContextWeakMutablePtr query_context;
ContextWeakMutablePtr session_context; /// Session context or nullptr. Could be equal to this.
ContextWeakMutablePtr global_context; /// Global context. Could be equal to this.
ContextWeakMutablePtr background_context; /// Context of background operations or a copy of global context. Could be equal to this.
/// XXX: move this stuff to shared part instead.
ContextMutablePtr buffer_context; /// Buffer context. Could be equal to this.
/// A flag, used to distinguish between user query and internal query to a database engine (MaterializedPostgreSQL).
bool is_internal_query = false;
/// A flag, used to detect sub-operations of background operations - in this case we won't need to build another background contexts
bool is_background_operation = false;
inline static ContextPtr global_context_instance;
inline static ContextPtr background_context_instance; /// Global holder to maintain ownership of background_context
/// Temporary data for query execution accounting.
TemporaryDataOnDiskScopePtr temp_data_on_disk;
/// Resource classifier for a query, holds smart pointers required for ResourceLink
/// NOTE: all resource links became invalid after `classifier` destruction
mutable ClassifierPtr classifier;
/// Prepared sets that can be shared between different queries. One use case is when is to share prepared sets between
/// mutation tasks of one mutation executed against different parts of the same table.
PreparedSetsCachePtr prepared_sets_cache;
struct StorageCache
{
static constexpr size_t NumShards = 6;
struct Shard
{
std::mutex mutex;
std::unordered_set<
StorageID,
StorageID::DatabaseAndTableNameHash,
StorageID::DatabaseAndTableNameEqual
> set;
};
std::array<Shard, NumShards> shards;
static size_t shardIndex(const StorageID & id)
{
return StorageID::DatabaseAndTableNameHash{}(id) & (NumShards - 1);
}
};
mutable StorageCache storage_cache;
/// Cache for reverse lookups of serialized dictionary keys used in `dictGetKeys` function.
/// This is a per query cache and not shared across queries.
mutable ReverseLookupCachePtr reverse_lookup_cache;
/// this is a mode of parallel replicas where we set parallel_replicas_count and parallel_replicas_offset
/// and generate specific filters on the replicas (e.g. when using parallel replicas with sample key)
/// if we already use a different mode of parallel replicas we want to disable this mode
bool offset_parallel_replicas_enabled = true;
/// Used at query runtime to save per-query runtime filters and find them by names
RuntimeFilterLookupPtr runtime_filter_lookup;
public:
/// Some counters for current query execution.
/// Most of them are workarounds and should be removed in the future.
struct KitchenSink
{
std::atomic<size_t> analyze_counter = 0;
KitchenSink() = default;
KitchenSink(const KitchenSink & rhs)
: analyze_counter(rhs.analyze_counter.load())
{}
KitchenSink & operator=(const KitchenSink & rhs)
{
if (&rhs == this)
return *this;
analyze_counter = rhs.analyze_counter.load();
return *this;
}
};
KitchenSink kitchen_sink;
void resetSharedContext();
protected:
using SampleBlockCache = std::unordered_map<std::string, SharedHeader>;
mutable SampleBlockCache sample_block_cache;
mutable std::mutex sample_block_cache_mutex;
QueryMetadataCacheWeakPtr query_metadata_cache;
PartUUIDsPtr part_uuids; /// set of parts' uuids, is used for query parts deduplication
PartUUIDsPtr ignored_part_uuids; /// set of parts' uuids are meant to be excluded from query processing
NameToNameMap query_parameters; /// Dictionary with query parameters for prepared statements.
/// (key=name, value)
IHostContextPtr host_context; /// Arbitrary object that may used to attach some host specific information to query context,
/// when using ClickHouse as a library in some project. For example, it may contain host
/// logger, some query identification information, profiling guards, etc. This field is
/// to be customized in HTTP and TCP servers by overloading the customizeContext(DB::ContextPtr)
/// methods.
ZooKeeperMetadataTransactionPtr metadata_transaction; /// Distributed DDL context. I'm not sure if it's a suitable place for this,
/// but it's the easiest way to pass this through the whole stack from executeQuery(...)
/// to DatabaseOnDisk::commitCreateTable(...) or IStorage::alter(...) without changing
/// thousands of signatures.
/// And I hope it will be replaced with more common Transaction sometime.
std::optional<UUID> parent_table_uuid; /// See comment on setParentTable().
StopToken ddl_query_cancellation; // See comment on setDDLQueryCancellation().
Coordination::Requests ddl_additional_checks_on_enqueue; // See comment on setDDLAdditionalChecksOnEnqueue().
MergeTreeTransactionPtr merge_tree_transaction; /// Current transaction context. Can be inside session or query context.
/// It's shared with all children contexts.
MergeTreeTransactionHolder merge_tree_transaction_holder; /// It will rollback or commit transaction on Context destruction.
std::shared_ptr<BackupsInMemoryHolder> backups_in_memory; /// Backups stored in memory (see "BACKUP ... TO Memory()" statement)
/// Use copy constructor or createGlobal() instead
ContextData();
ContextData(const ContextData &);
mutable ThrottlerPtr remote_read_query_throttler; /// A query-wide throttler for remote IO reads
mutable ThrottlerPtr remote_write_query_throttler; /// A query-wide throttler for remote IO writes
mutable ThrottlerPtr local_read_query_throttler; /// A query-wide throttler for local IO reads
mutable ThrottlerPtr local_write_query_throttler; /// A query-wide throttler for local IO writes
mutable ThrottlerPtr backups_query_throttler; /// A query-wide throttler for BACKUPs
mutable std::mutex mutex_shared_context; /// mutex to avoid accessing destroyed shared context pointer
/// some Context methods can be called after the shared context is destroyed
/// example, Context::handleCrash() method - called from signal handler
};
/** A set of known objects that can be used in the query.
* Consists of a shared part (always common to all sessions and queries)
* and copied part (which can be its own for each session or query).
*
* Everything is encapsulated for all sorts of checks and locks.
*/
class Context: public ContextData, public std::enable_shared_from_this<Context>
{
private:
/// ContextData mutex
mutable ContextSharedMutex mutex;
Context();
Context(const Context &);
#if USE_NURAFT
void setKeeperDispatcher(std::shared_ptr<KeeperDispatcher> dispatcher) const;
#endif
public:
/// Create initial Context with ContextShared and etc.
static ContextMutablePtr createGlobal(ContextSharedPart * shared_part);
static ContextMutablePtr createCopy(const ContextWeakPtr & other);
static ContextMutablePtr createCopy(const ContextMutablePtr & other);
static ContextMutablePtr createCopy(const ContextPtr & other);
static SharedContextHolder createShared();
~Context();
String getPath() const;
String getFlagsPath() const;
String getUserFilesPath() const;
String getDictionariesLibPath() const;
String getUserScriptsPath() const;
String getFilesystemCachesPath() const;
String getFilesystemCacheUser() const;
DatabaseAndTable getOrCacheStorage(const StorageID & id, std::function<DatabaseAndTable()> storage_getter) const;
// Get the disk used by databases to store metadata files.
std::shared_ptr<IDisk> getDatabaseDisk() const;
/// Different kinds of warnings available for use with the `system.warnings` table.
/// More can be added as necessary. These are used to track if a warning is already
/// present to be able to add, remove or update warnings from the table
enum class WarningType
{
AST_FUZZER_IS_ENABLED,
AVAILABLE_DISK_SPACE_TOO_LOW_FOR_DATA,
AVAILABLE_DISK_SPACE_TOO_LOW_FOR_LOGS,
AVAILABLE_MEMORY_TOO_LOW,
DB_ORDINARY_DEPRECATED,
DELAY_ACCOUNTING_DISABLED,
LINUX_FAST_CLOCK_SOURCE_NOT_USED,
LINUX_MAX_PID_TOO_LOW,
LINUX_MAX_THREADS_COUNT_TOO_LOW,
LINUX_MEMORY_OVERCOMMIT_DISABLED,
LINUX_TRANSPARENT_HUGEPAGES_SET_TO_ALWAYS,
MAX_ACTIVE_PARTS,
MAX_ATTACHED_DATABASES,
MAX_ATTACHED_DICTIONARIES,
MAX_ATTACHED_TABLES,
MAX_ATTACHED_VIEWS,
MAX_NAMED_COLLECTIONS,
MAX_NUM_THREADS_LOWER_THAN_LIMIT,
MAX_PENDING_MUTATIONS_EXCEEDS_LIMIT,
MAX_PENDING_MUTATIONS_OVER_THRESHOLD,
MAYBE_BROKEN_TABLES,
OBSOLETE_MONGO_TABLE_DEFINITION,
OBSOLETE_SETTINGS,
PROCESS_USER_MATCHES_DATA_OWNER,
RABBITMQ_UNSUPPORTED_COLUMNS,
REPLICATED_DB_WITH_ALL_GROUPS_CLUSTER_PREFIX,
ROTATIONAL_DISK_WITH_DISABLED_READHEAD,
SERVER_BUILT_IN_DEBUG_MODE,
SERVER_BUILT_WITH_COVERAGE,
SERVER_BUILT_WITH_SANITIZERS,
SERVER_CPU_OVERLOAD,
SERVER_LOGGING_LEVEL_TEST,
SERVER_MEMORY_OVERLOAD,
SERVER_RUN_UNDER_DEBUGGER,
SETTING_ZERO_COPY_REPLICATION_ENABLED,
SKIPPING_CONDITION_QUERY,
THREAD_FUZZER_IS_ENABLED,
};
std::unordered_map<WarningType, PreformattedMessage> getWarnings() const;
void addOrUpdateWarningMessage(WarningType warning, const PreformattedMessage & message) const;
void addWarningMessageAboutDatabaseOrdinary(const String & database_name) const;
void removeWarningMessage(WarningType warning) const;
void removeAllWarnings() const;
VolumePtr getGlobalTemporaryVolume() const; /// TODO: remove, use `getTempDataOnDisk`
TemporaryDataOnDiskScopePtr getTempDataOnDisk() const;
TemporaryDataOnDiskScopePtr getSharedTempDataOnDisk() const;
void setTempDataOnDisk(TemporaryDataOnDiskScopePtr temp_data_on_disk_);
void setFilesystemCachesPath(const String & path);
void setFilesystemCacheUser(const String & user);
void setPath(const String & path);
void setFlagsPath(const String & path);
void setUserFilesPath(const String & path);
void setDictionariesLibPath(const String & path);
void setUserScriptsPath(const String & path);
void setTemporaryStorageInCache(const String & cache_disk_name, size_t max_size);
void setTemporaryStoragePolicy(const String & policy_name, size_t max_size);
void setTemporaryStoragePath(const String & path, size_t max_size);
#if !ENABLE_DISTRIBUTED_CACHE
[[noreturn]]
#endif
void setTemporaryStorageInDistributedCache(size_t max_size);
using ConfigurationPtr = Poco::AutoPtr<Poco::Util::AbstractConfiguration>;
/// Global application configuration settings.
void setConfig(const ConfigurationPtr & config);
const Poco::Util::AbstractConfiguration & getConfigRef() const;
AccessControl & getAccessControl();
const AccessControl & getAccessControl() const;
/// Sets external authenticators config (LDAP, Kerberos).
void setExternalAuthenticatorsConfig(const Poco::Util::AbstractConfiguration & config);
/// Creates GSSAcceptorContext instance based on external authenticator params.
std::unique_ptr<GSSAcceptorContext> makeGSSAcceptorContext() const;
/** Take the list of users, quotas and configuration profiles from this config.
* The list of users is completely replaced.
* The accumulated quota values are not reset if the quota is not deleted.
*/
void setUsersConfig(const ConfigurationPtr & config);
ConfigurationPtr getUsersConfig();
/// Sets the current user, assuming they are already authenticated.
/// WARNING: This function doesn't check the password!
void setUser(const UUID & user_id_, const std::vector<UUID> & external_roles_ = {});
UserPtr getUser() const;
std::optional<UUID> getUserID() const;
String getUserName() const;
void setCurrentRoles(const Strings & new_current_roles, bool check_grants = true);
void setCurrentRoles(const std::vector<UUID> & new_current_roles, bool check_grants = true);
void setCurrentRoles(const RolesOrUsersSet & new_current_roles, bool check_grants = true);
void setCurrentRolesDefault();
std::vector<UUID> getCurrentRoles() const;
std::vector<UUID> getEnabledRoles() const;
std::shared_ptr<const EnabledRolesInfo> getRolesInfo() const;
void setCurrentProfile(const String & profile_name, bool check_constraints = true);
void setCurrentProfile(const UUID & profile_id, bool check_constraints = true);
void setCurrentProfiles(const SettingsProfilesInfo & profiles_info, bool check_constraints = true);
std::vector<UUID> getCurrentProfiles() const;
std::vector<UUID> getEnabledProfiles() const;
/// Checks access rights.
/// Empty database means the current database.
void checkAccess(const AccessFlags & flags) const;
void checkAccess(const AccessFlags & flags, std::string_view database) const;
void checkAccess(const AccessFlags & flags, std::string_view database, std::string_view table) const;
void checkAccess(const AccessFlags & flags, std::string_view database, std::string_view table, std::string_view column) const;
void checkAccess(const AccessFlags & flags, std::string_view database, std::string_view table, const std::vector<std::string_view> & columns) const;
void checkAccess(const AccessFlags & flags, std::string_view database, std::string_view table, const Strings & columns) const;
void checkAccess(const AccessFlags & flags, const StorageID & table_id) const;
void checkAccess(const AccessFlags & flags, const StorageID & table_id, std::string_view column) const;
void checkAccess(const AccessFlags & flags, const StorageID & table_id, const std::vector<std::string_view> & columns) const;
void checkAccess(const AccessFlags & flags, const StorageID & table_id, const Strings & columns) const;
void checkAccess(const AccessRightsElement & element) const;
void checkAccess(const AccessRightsElements & elements) const;
std::shared_ptr<const ContextAccessWrapper> getAccess() const;
RowPolicyFilterPtr getRowPolicyFilter(const String & database, const String & table_name, RowPolicyFilterType filter_type) const;
std::shared_ptr<const EnabledQuota> getQuota() const;
std::optional<QuotaUsage> getQuotaUsage() const;
/// Resource management related
ResourceManagerPtr getResourceManager() const;
ClassifierPtr getWorkloadClassifier() const;
void releaseQuerySlot() const;
String getMergeWorkload() const;
void setMergeWorkload(const String & value);
String getLicenseFile() const;
void setLicenseFile(const String & value);
String getMutationWorkload() const;
void setMutationWorkload(const String & value);
bool getThrowOnUnknownWorkload() const;
void setThrowOnUnknownWorkload(bool value);
bool getCPUSlotPreemption() const;
UInt64 getCPUSlotQuantum() const;
UInt64 getCPUSlotPreemptionTimeout() const;
void setCPUSlotPreemption(bool cpu_slot_preemption, UInt64 cpu_slot_quantum_ns, UInt64 cpu_slot_preemption_timeout_ms);
UInt64 getConcurrentThreadsSoftLimitNum() const;
UInt64 getConcurrentThreadsSoftLimitRatioToCores() const;
String getConcurrentThreadsScheduler() const;
std::pair<UInt64, String> setConcurrentThreadsSoftLimit(UInt64 num, UInt64 ratio_to_cores, const String & scheduler);
/// We have to copy external tables inside executeQuery() to track limits. Therefore, set callback for it. Must set once.
void setExternalTablesInitializer(ExternalTablesInitializer && initializer);
/// This method is called in executeQuery() and will call the external tables initializer.
void initializeExternalTablesIfSet();
/// This is a callback which returns deserialized QueryPlan if the packet with QueryPlan was received.
void setQueryPlanDeserializationCallback(QueryPlanDeserializationCallback && callback);
/// This method is called in executeQuery() and will call the query plan deserialization callback.
std::shared_ptr<QueryPlanAndSets> getDeserializedQueryPlan();
/// When input() is present we have to send columns structure to client
void setInputInitializer(InputInitializer && initializer);
/// This method is called in StorageInput::read while executing query
void initializeInput(const StoragePtr & input_storage);
/// Callback for read data blocks from client one by one for function input()
void setInputBlocksReaderCallback(InputBlocksReader && reader);
/// Get callback for reading data for input()
InputBlocksReader getInputBlocksReaderCallback() const;
void resetInputCallbacks();
/// Clear cached table function results (e.g. StorageInput) to avoid stale state across queries.
void clearTableFunctionResults();
/// Returns information about the client executing a query.
const ClientInfo & getClientInfo() const { return client_info; }
/// Modify stored in the context information about the client executing a query.
void setClientInfo(const ClientInfo & client_info_);
void setClientName(const String & client_name);
void setClientInterface(ClientInfo::Interface interface);
void setClientVersion(UInt64 client_version_major, UInt64 client_version_minor, UInt64 client_version_patch, unsigned client_tcp_protocol_version);
void setClientConnectionId(uint32_t connection_id);
void setScriptQueryAndLineNumber(uint32_t query_number, uint32_t line_number);
void setHTTPClientInfo(const Poco::Net::HTTPRequest & request);
void setForwardedFor(const String & forwarded_for);
void setQueryKind(ClientInfo::QueryKind query_kind);
void setQueryKindInitial();
void setQueryKindReplicatedDatabaseInternal();
void setCurrentUserName(const String & current_user_name);
void setCurrentAddress(const Poco::Net::SocketAddress & current_address);
void setInitialUserName(const String & initial_user_name);
void setInitialAddress(const Poco::Net::SocketAddress & initial_address);
void setInitialQueryId(const String & initial_query_id);
void setInitialQueryStartTime(std::chrono::time_point<std::chrono::system_clock> initial_query_start_time);
void setQuotaClientKey(const String & quota_key);
void setConnectionClientVersion(UInt64 client_version_major, UInt64 client_version_minor, UInt64 client_version_patch, unsigned client_tcp_protocol_version);
void increaseDistributedDepth();
const OpenTelemetry::TracingContext & getClientTraceContext() const { return client_info.client_trace_context; }
OpenTelemetry::TracingContext & getClientTraceContext() { return client_info.client_trace_context; }
enum StorageNamespace
{
ResolveGlobal = 1u, /// Database name must be specified
ResolveCurrentDatabase = 2u, /// Use current database
ResolveOrdinary = ResolveGlobal | ResolveCurrentDatabase, /// If database name is not specified, use current database
ResolveExternal = 4u, /// Try get external table
ResolveAll = ResolveExternal | ResolveOrdinary /// If database name is not specified, try get external table,
/// if external table not found use current database.
};
String resolveDatabase(const String & database_name) const;
StorageID resolveStorageID(StorageID storage_id, StorageNamespace where = StorageNamespace::ResolveAll) const;
StorageID tryResolveStorageID(StorageID storage_id, StorageNamespace where = StorageNamespace::ResolveAll) const;
StorageID resolveStorageIDImpl(StorageID storage_id, StorageNamespace where, std::optional<Exception> * exception) const;
Tables getExternalTables() const;
void addExternalTable(const String & table_name, TemporaryTableHolder && temporary_table);
void updateExternalTable(const String & table_name, TemporaryTableHolder && temporary_table);
void addOrUpdateExternalTable(const String & table_name, TemporaryTableHolder && temporary_table);
void addExternalTable(const String & table_name, std::shared_ptr<TemporaryTableHolder> temporary_table);
void updateExternalTable(const String & table_name, std::shared_ptr<TemporaryTableHolder> temporary_table);
void addOrUpdateExternalTable(const String & table_name, std::shared_ptr<TemporaryTableHolder> temporary_table);
std::shared_ptr<TemporaryTableHolder> findExternalTable(const String & table_name) const;
std::shared_ptr<TemporaryTableHolder> removeExternalTable(const String & table_name);
Scalars getScalars() const;
Block getScalar(const String & name) const;
void addScalar(const String & name, const Block & block);
bool hasScalar(const String & name) const;
std::optional<Block> tryGetSpecialScalar(const String & name) const;
void addSpecialScalar(const String & name, const Block & block);
/// Mapping between identifiers and time series tags collected in the context of the currently executed query.
std::shared_ptr<const ContextTimeSeriesTagsCollector> getTimeSeriesTagsCollector() const;
std::shared_ptr<ContextTimeSeriesTagsCollector> getTimeSeriesTagsCollector();
const QueryAccessInfo & getQueryAccessInfo() const { return *getQueryAccessInfoPtr(); }
QueryAccessInfoPtr getQueryAccessInfoPtr() const { return query_access_info; }
void setQueryAccessInfo(QueryAccessInfoPtr other) { query_access_info = other; }
void addQueryAccessInfo(
const StorageID & table_id,
const Names & column_names);
void addQueryAccessInfo(
const String & quoted_database_name,
const String & full_quoted_table_name,
const Names & column_names);
void addQueryAccessInfo(const Names & partition_names);
void addViewAccessInfo(const String & view_name);
void addUsedRowPolicy(const String & policy_name);