Skip to main content

common_catalog/
consts.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use const_format::concatcp;
16
17pub const SYSTEM_CATALOG_NAME: &str = "system";
18pub const INFORMATION_SCHEMA_NAME: &str = "information_schema";
19pub const PG_CATALOG_NAME: &str = "pg_catalog";
20pub const SYSTEM_CATALOG_TABLE_NAME: &str = "system_catalog";
21pub const DEFAULT_CATALOG_NAME: &str = env!("DEFAULT_CATALOG_NAME");
22pub const DEFAULT_SCHEMA_NAME: &str = "public";
23pub const DEFAULT_PRIVATE_SCHEMA_NAME: &str = concatcp!(DEFAULT_CATALOG_NAME, "_private");
24
25/// Reserves [0,MIN_USER_FLOW_ID) for internal usage.
26/// User defined table id starts from this value.
27pub const MIN_USER_FLOW_ID: u32 = 1024;
28/// Reserves [0,MIN_USER_TABLE_ID) for internal usage.
29/// User defined table id starts from this value.
30pub const MIN_USER_TABLE_ID: u32 = 1024;
31/// the max system table id
32pub const MAX_SYS_TABLE_ID: u32 = MIN_USER_TABLE_ID - 1;
33/// system_catalog table id
34pub const SYSTEM_CATALOG_TABLE_ID: u32 = 0;
35/// scripts table id
36pub const SCRIPTS_TABLE_ID: u32 = 1;
37/// numbers table id
38pub const NUMBERS_TABLE_ID: u32 = 2;
39
40/// ----- Begin of information_schema tables -----
41/// id for information_schema.tables
42pub const INFORMATION_SCHEMA_TABLES_TABLE_ID: u32 = 3;
43/// id for information_schema.columns
44pub const INFORMATION_SCHEMA_COLUMNS_TABLE_ID: u32 = 4;
45/// id for information_schema.engines
46pub const INFORMATION_SCHEMA_ENGINES_TABLE_ID: u32 = 5;
47/// id for information_schema.column_privileges
48pub const INFORMATION_SCHEMA_COLUMN_PRIVILEGES_TABLE_ID: u32 = 6;
49/// id for information_schema.column_statistics
50pub const INFORMATION_SCHEMA_COLUMN_STATISTICS_TABLE_ID: u32 = 7;
51/// id for information_schema.build_info
52pub const INFORMATION_SCHEMA_BUILD_INFO_TABLE_ID: u32 = 8;
53/// id for information_schema.CHARACTER_SETS
54pub const INFORMATION_SCHEMA_CHARACTER_SETS_TABLE_ID: u32 = 9;
55/// id for information_schema.COLLATIONS
56pub const INFORMATION_SCHEMA_COLLATIONS_TABLE_ID: u32 = 10;
57/// id for information_schema.COLLATIONS
58pub const INFORMATION_SCHEMA_COLLATION_CHARACTER_SET_APPLICABILITY_TABLE_ID: u32 = 11;
59/// id for information_schema.CHECK_CONSTRAINTS
60pub const INFORMATION_SCHEMA_CHECK_CONSTRAINTS_TABLE_ID: u32 = 12;
61/// id for information_schema.EVENTS
62pub const INFORMATION_SCHEMA_EVENTS_TABLE_ID: u32 = 13;
63/// id for information_schema.FILES
64pub const INFORMATION_SCHEMA_FILES_TABLE_ID: u32 = 14;
65/// id for information_schema.SCHEMATA
66pub const INFORMATION_SCHEMA_SCHEMATA_TABLE_ID: u32 = 15;
67/// id for information_schema.KEY_COLUMN_USAGE
68pub const INFORMATION_SCHEMA_KEY_COLUMN_USAGE_TABLE_ID: u32 = 16;
69/// id for information_schema.OPTIMIZER_TRACE
70pub const INFORMATION_SCHEMA_OPTIMIZER_TRACE_TABLE_ID: u32 = 17;
71/// id for information_schema.PARAMETERS
72pub const INFORMATION_SCHEMA_PARAMETERS_TABLE_ID: u32 = 18;
73/// id for information_schema.PROFILING
74pub const INFORMATION_SCHEMA_PROFILING_TABLE_ID: u32 = 19;
75/// id for information_schema.REFERENTIAL_CONSTRAINTS
76pub const INFORMATION_SCHEMA_REFERENTIAL_CONSTRAINTS_TABLE_ID: u32 = 20;
77/// id for information_schema.ROUTINES
78pub const INFORMATION_SCHEMA_ROUTINES_TABLE_ID: u32 = 21;
79/// id for information_schema.SCHEMA_PRIVILEGES
80pub const INFORMATION_SCHEMA_SCHEMA_PRIVILEGES_TABLE_ID: u32 = 22;
81/// id for information_schema.TABLE_PRIVILEGES
82pub const INFORMATION_SCHEMA_TABLE_PRIVILEGES_TABLE_ID: u32 = 23;
83/// id for information_schema.TRIGGERS (for mysql)
84pub const INFORMATION_SCHEMA_TRIGGERS_TABLE_ID: u32 = 24;
85/// id for information_schema.GLOBAL_STATUS
86pub const INFORMATION_SCHEMA_GLOBAL_STATUS_TABLE_ID: u32 = 25;
87/// id for information_schema.SESSION_STATUS
88pub const INFORMATION_SCHEMA_SESSION_STATUS_TABLE_ID: u32 = 26;
89/// id for information_schema.PARTITIONS
90pub const INFORMATION_SCHEMA_PARTITIONS_TABLE_ID: u32 = 28;
91/// id for information_schema.REGION_PEERS
92pub const INFORMATION_SCHEMA_REGION_PEERS_TABLE_ID: u32 = 29;
93/// id for information_schema.columns
94pub const INFORMATION_SCHEMA_TABLE_CONSTRAINTS_TABLE_ID: u32 = 30;
95/// id for information_schema.cluster_info
96pub const INFORMATION_SCHEMA_CLUSTER_INFO_TABLE_ID: u32 = 31;
97/// id for information_schema.VIEWS
98pub const INFORMATION_SCHEMA_VIEW_TABLE_ID: u32 = 32;
99/// id for information_schema.FLOWS
100pub const INFORMATION_SCHEMA_FLOW_TABLE_ID: u32 = 33;
101/// id for information_schema.procedure_info
102pub const INFORMATION_SCHEMA_PROCEDURE_INFO_TABLE_ID: u32 = 34;
103/// id for information_schema.region_statistics
104pub const INFORMATION_SCHEMA_REGION_STATISTICS_TABLE_ID: u32 = 35;
105/// id for information_schema.process_list
106pub const INFORMATION_SCHEMA_PROCESS_LIST_TABLE_ID: u32 = 36;
107/// id for information_schema.ssts_manifest
108pub const INFORMATION_SCHEMA_SSTS_MANIFEST_TABLE_ID: u32 = 37;
109/// id for information_schema.ssts_storage
110pub const INFORMATION_SCHEMA_SSTS_STORAGE_TABLE_ID: u32 = 38;
111/// id for information_schema.ssts_index_meta
112pub const INFORMATION_SCHEMA_SSTS_INDEX_META_TABLE_ID: u32 = 39;
113/// id for information_schema.alerts
114pub const INFORMATION_SCHEMA_ALERTS_TABLE_ID: u32 = 40;
115/// id for information_schema.region_info
116pub const INFORMATION_SCHEMA_REGION_INFO_TABLE_ID: u32 = 41;
117/// id for information_schema.table_semantics
118pub const INFORMATION_SCHEMA_TABLE_SEMANTICS_TABLE_ID: u32 = 42;
119/// id for information_schema.statistics
120pub const INFORMATION_SCHEMA_STATISTICS_TABLE_ID: u32 = 43;
121/// id for information_schema.recycle_bin
122pub const INFORMATION_SCHEMA_RECYCLE_BIN_TABLE_ID: u32 = 44;
123/// id for information_schema.flow_statistics
124pub const INFORMATION_SCHEMA_FLOW_STATISTICS_TABLE_ID: u32 = 45;
125/// id for information_schema.PLUGINS
126pub const INFORMATION_SCHEMA_PLUGINS_TABLE_ID: u32 = 47;
127/// id for information_schema.USER_PRIVILEGES
128pub const INFORMATION_SCHEMA_USER_PRIVILEGES_TABLE_ID: u32 = 48;
129/// id for information_schema.PROCESSLIST (for mysql)
130pub const INFORMATION_SCHEMA_PROCESSLIST_TABLE_ID: u32 = 49;
131
132// ----- End of information_schema tables -----
133
134// Computed system tables under `greptime_private` (same virtual-table id space
135// as the information_schema tables above).
136/// id for greptime_private.semantic_entities (computed entity registry)
137pub const SEMANTIC_ENTITIES_TABLE_ID: u32 = 45;
138/// id for greptime_private.semantic_relationships (computed edge set)
139pub const SEMANTIC_RELATIONSHIPS_TABLE_ID: u32 = 46;
140
141/// ----- Begin of pg_catalog tables -----
142pub const PG_CATALOG_TABLE_ID_START: u32 = 256;
143// Please leave at 128 table ids for Postgres
144// ----- End of pg_catalog tables -----
145
146pub const MITO_ENGINE: &str = "mito";
147pub const MITO2_ENGINE: &str = "mito2";
148pub const METRIC_ENGINE: &str = "metric";
149
150pub fn default_engine() -> &'static str {
151    MITO_ENGINE
152}
153
154pub const FILE_ENGINE: &str = "file";
155pub const RELATIONAL_ENGINE: &str = "relational";
156
157pub const SEMANTIC_TYPE_PRIMARY_KEY: &str = "TAG";
158pub const SEMANTIC_TYPE_FIELD: &str = "FIELD";
159pub const SEMANTIC_TYPE_TIME_INDEX: &str = "TIMESTAMP";
160
161const SYSTEM_SCHEMA_NAMES: [&str; 3] = [
162    INFORMATION_SCHEMA_NAME,
163    PG_CATALOG_NAME,
164    DEFAULT_PRIVATE_SCHEMA_NAME,
165];
166
167/// Returns the canonical name of the system schema `schema` refers to, ignoring ASCII
168/// case, or `None` if it is not one.
169///
170/// Only system schemas are matched case-insensitively, as MySQL does; user schema names
171/// keep the case they were created with.
172pub fn system_schema_name(schema: &str) -> Option<&'static str> {
173    SYSTEM_SCHEMA_NAMES
174        .into_iter()
175        .find(|name| schema.eq_ignore_ascii_case(name))
176}
177
178pub fn is_readonly_schema(schema: &str) -> bool {
179    matches!(schema, INFORMATION_SCHEMA_NAME)
180}
181
182/// Returns true for tables that must reject DDL/DML: everything in a read-only
183/// schema, plus computed system tables overlaid on a writable schema (the
184/// entity-graph tables under `greptime_private`).
185pub fn is_readonly_table(schema: &str, table: &str) -> bool {
186    is_readonly_schema(schema)
187        || (schema == DEFAULT_PRIVATE_SCHEMA_NAME
188            && matches!(
189                table,
190                SEMANTIC_ENTITIES_TABLE_NAME | SEMANTIC_RELATIONSHIPS_TABLE_NAME
191            ))
192}
193
194/// Returns true for tables whose *definition* is system-owned while their rows
195/// are user-written: created with a canonical schema on first write; user
196/// CREATE/ALTER/RENAME-into rejected; DML and DROP/TRUNCATE allowed (the next
197/// write recreates the table). Currently only the declared-edge table.
198pub fn is_ddl_reserved_table(schema: &str, table: &str) -> bool {
199    schema == DEFAULT_PRIVATE_SCHEMA_NAME && table == SEMANTIC_RELATIONSHIPS_DECLARED_TABLE_NAME
200}
201
202// ---- special table and fields ----
203pub const TRACE_ID_COLUMN: &str = "trace_id";
204pub const SPAN_ID_COLUMN: &str = "span_id";
205pub const SPAN_NAME_COLUMN: &str = "span_name";
206/// Column containing span attributes as JSON/JSON2.
207pub const SPAN_ATTRIBUTES_COLUMN: &str = "span_attributes";
208/// Column containing instrumentation scope attributes as JSON2.
209pub const SCOPE_ATTRIBUTES_COLUMN: &str = "scope_attributes";
210/// Column containing resource attributes as JSON2.
211pub const RESOURCE_ATTRIBUTES_COLUMN: &str = "resource_attributes";
212pub const SERVICE_NAME_COLUMN: &str = "service_name";
213pub const PARENT_SPAN_ID_COLUMN: &str = "parent_span_id";
214// More fixed columns/values of the `greptime_trace_v1` data model, shared by
215// the ingest side (`servers::otlp::trace` re-exports them) and the read-time
216// graph derivation so the two cannot silently drift.
217pub const TRACE_TIMESTAMP_COLUMN: &str = "timestamp";
218pub const SPAN_KIND_COLUMN: &str = "span_kind";
219pub const SPAN_STATUS_CODE_COLUMN: &str = "span_status_code";
220pub const DURATION_NANO_COLUMN: &str = "duration_nano";
221pub const TIMESTAMP_END_COLUMN: &str = "timestamp_end";
222pub const SPAN_KIND_CLIENT: &str = "SPAN_KIND_CLIENT";
223pub const SPAN_KIND_SERVER: &str = "SPAN_KIND_SERVER";
224pub const SPAN_STATUS_ERROR: &str = "STATUS_CODE_ERROR";
225pub const TRACE_TABLE_NAME: &str = "opentelemetry_traces";
226pub const TRACE_TABLE_NAME_SESSION_KEY: &str = "trace_table_name";
227// ---- End of special table and fields ----
228
229/// Generate the trace services table name from the trace table name by adding `_services` suffix.
230pub fn trace_services_table_name(trace_table_name: &str) -> String {
231    format!("{}_services", trace_table_name)
232}
233
234/// Generate the trace operations table name from the trace table name by adding `_operations` suffix.
235pub fn trace_operations_table_name(trace_table_name: &str) -> String {
236    format!("{}_operations", trace_table_name)
237}
238// ---- End of special table and fields ----
239
240// ---- Entity relationship graph tables (live in `greptime_private`) ----
241// The `semantic_` prefix ties them to the semantic layer they derive from
242// (`greptime.semantic.entity.*` declarations, `information_schema.table_semantics`);
243// no `__` prefix is needed since `greptime_private` already scopes them.
244/// Computed entity registry: the node set of the observability graph, derived at
245/// read time from tables that declared `greptime.semantic.entity.*` identities.
246pub const SEMANTIC_ENTITIES_TABLE_NAME: &str = "semantic_entities";
247/// Computed relationship set: the edge set of the observability graph, derived at
248/// read time (`calls`/`runs_on`/... ) and unioned with the declared-edge table.
249pub const SEMANTIC_RELATIONSHIPS_TABLE_NAME: &str = "semantic_relationships";
250/// Physical table of hand-declared edges, unioned into the computed
251/// `semantic_relationships`; see [`is_ddl_reserved_table`] for its lifecycle.
252pub const SEMANTIC_RELATIONSHIPS_DECLARED_TABLE_NAME: &str = "semantic_relationships_declared";
253
254/// Width of the window the graph derivation bins observations into. Sources
255/// synthesizing observations must land a row in every window they describe,
256/// so this is shared rather than restated per crate.
257pub const SEMANTIC_GRAPH_WINDOW_NANOS: i64 = 60 * 1_000_000_000;
258
259// Column names of the graph tables: `catalog` exposes these schemas and the
260// read-time plans in `operator` must project exactly them.
261pub const OBSERVED_AT_COLUMN: &str = "observed_at";
262pub const WINDOW_START_COLUMN: &str = "window_start";
263pub const WINDOW_END_COLUMN: &str = "window_end";
264pub const FRESH_UNTIL_COLUMN: &str = "fresh_until";
265pub const ENTITY_TYPE_COLUMN: &str = "entity_type";
266pub const ENTITY_ID_COLUMN: &str = "entity_id";
267pub const ENTITY_ID_ATTRS_COLUMN: &str = "entity_id_attrs";
268pub const ENTITY_SCOPE_COLUMN: &str = "scope";
269pub const ENTITY_DESCRIPTIVE_COLUMN: &str = "descriptive";
270pub const SOURCE_TABLES_COLUMN: &str = "source_tables";
271pub const SRC_TYPE_COLUMN: &str = "src_type";
272pub const SRC_ID_COLUMN: &str = "src_id";
273pub const DST_TYPE_COLUMN: &str = "dst_type";
274pub const DST_ID_COLUMN: &str = "dst_id";
275pub const REL_TYPE_COLUMN: &str = "rel_type";
276pub const PROVENANCE_COLUMN: &str = "provenance";
277pub const CONFIDENCE_COLUMN: &str = "confidence";
278pub const REQUEST_COUNT_COLUMN: &str = "request_count";
279pub const UNMATCHED_COUNT_COLUMN: &str = "unmatched_count";
280pub const ERROR_COUNT_COLUMN: &str = "error_count";
281pub const DURATION_SUM_COLUMN: &str = "duration_sum";
282pub const DURATION_COUNT_COLUMN: &str = "duration_count";
283pub const DURATION_MAX_COLUMN: &str = "duration_max";
284pub const EDGE_ATTRIBUTES_COLUMN: &str = "attributes";
285// Declared-edge table only.
286pub const VALID_FROM_COLUMN: &str = "valid_from";
287pub const VALID_UNTIL_COLUMN: &str = "valid_until";
288pub const GENERATION_ID_COLUMN: &str = "generation_id";
289// ---- End of entity relationship graph tables ----