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
126// ----- End of information_schema tables -----
127
128// Computed system tables under `greptime_private` (same virtual-table id space
129// as the information_schema tables above).
130/// id for greptime_private.semantic_entities (computed entity registry)
131pub const SEMANTIC_ENTITIES_TABLE_ID: u32 = 45;
132/// id for greptime_private.semantic_relationships (computed edge set)
133pub const SEMANTIC_RELATIONSHIPS_TABLE_ID: u32 = 46;
134
135/// ----- Begin of pg_catalog tables -----
136pub const PG_CATALOG_TABLE_ID_START: u32 = 256;
137// Please leave at 128 table ids for Postgres
138// ----- End of pg_catalog tables -----
139
140pub const MITO_ENGINE: &str = "mito";
141pub const MITO2_ENGINE: &str = "mito2";
142pub const METRIC_ENGINE: &str = "metric";
143
144pub fn default_engine() -> &'static str {
145    MITO_ENGINE
146}
147
148pub const FILE_ENGINE: &str = "file";
149
150pub const SEMANTIC_TYPE_PRIMARY_KEY: &str = "TAG";
151pub const SEMANTIC_TYPE_FIELD: &str = "FIELD";
152pub const SEMANTIC_TYPE_TIME_INDEX: &str = "TIMESTAMP";
153
154const SYSTEM_SCHEMA_NAMES: [&str; 3] = [
155    INFORMATION_SCHEMA_NAME,
156    PG_CATALOG_NAME,
157    DEFAULT_PRIVATE_SCHEMA_NAME,
158];
159
160/// Returns the canonical name of the system schema `schema` refers to, ignoring ASCII
161/// case, or `None` if it is not one.
162///
163/// Only system schemas are matched case-insensitively, as MySQL does; user schema names
164/// keep the case they were created with.
165pub fn system_schema_name(schema: &str) -> Option<&'static str> {
166    SYSTEM_SCHEMA_NAMES
167        .into_iter()
168        .find(|name| schema.eq_ignore_ascii_case(name))
169}
170
171pub fn is_readonly_schema(schema: &str) -> bool {
172    matches!(schema, INFORMATION_SCHEMA_NAME)
173}
174
175/// Returns true for tables that must reject DDL/DML: everything in a read-only
176/// schema, plus computed system tables overlaid on a writable schema (the
177/// entity-graph tables under `greptime_private`).
178pub fn is_readonly_table(schema: &str, table: &str) -> bool {
179    is_readonly_schema(schema)
180        || (schema == DEFAULT_PRIVATE_SCHEMA_NAME
181            && matches!(
182                table,
183                SEMANTIC_ENTITIES_TABLE_NAME | SEMANTIC_RELATIONSHIPS_TABLE_NAME
184            ))
185}
186
187/// Returns true for tables whose *definition* is system-owned while their rows
188/// are user-written: created with a canonical schema on first write; user
189/// CREATE/ALTER/RENAME-into rejected; DML and DROP/TRUNCATE allowed (the next
190/// write recreates the table). Currently only the declared-edge table.
191pub fn is_ddl_reserved_table(schema: &str, table: &str) -> bool {
192    schema == DEFAULT_PRIVATE_SCHEMA_NAME && table == SEMANTIC_RELATIONSHIPS_DECLARED_TABLE_NAME
193}
194
195// ---- special table and fields ----
196pub const TRACE_ID_COLUMN: &str = "trace_id";
197pub const SPAN_ID_COLUMN: &str = "span_id";
198pub const SPAN_NAME_COLUMN: &str = "span_name";
199pub const SERVICE_NAME_COLUMN: &str = "service_name";
200pub const PARENT_SPAN_ID_COLUMN: &str = "parent_span_id";
201// More fixed columns/values of the `greptime_trace_v1` data model, shared by
202// the ingest side (`servers::otlp::trace` re-exports them) and the read-time
203// graph derivation so the two cannot silently drift.
204pub const TRACE_TIMESTAMP_COLUMN: &str = "timestamp";
205pub const SPAN_KIND_COLUMN: &str = "span_kind";
206pub const SPAN_STATUS_CODE_COLUMN: &str = "span_status_code";
207pub const DURATION_NANO_COLUMN: &str = "duration_nano";
208pub const SPAN_KIND_CLIENT: &str = "SPAN_KIND_CLIENT";
209pub const SPAN_KIND_SERVER: &str = "SPAN_KIND_SERVER";
210pub const SPAN_STATUS_ERROR: &str = "STATUS_CODE_ERROR";
211pub const TRACE_TABLE_NAME: &str = "opentelemetry_traces";
212pub const TRACE_TABLE_NAME_SESSION_KEY: &str = "trace_table_name";
213// ---- End of special table and fields ----
214
215/// Generate the trace services table name from the trace table name by adding `_services` suffix.
216pub fn trace_services_table_name(trace_table_name: &str) -> String {
217    format!("{}_services", trace_table_name)
218}
219
220/// Generate the trace operations table name from the trace table name by adding `_operations` suffix.
221pub fn trace_operations_table_name(trace_table_name: &str) -> String {
222    format!("{}_operations", trace_table_name)
223}
224// ---- End of special table and fields ----
225
226// ---- Entity relationship graph tables (live in `greptime_private`) ----
227// The `semantic_` prefix ties them to the semantic layer they derive from
228// (`greptime.semantic.entity.*` declarations, `information_schema.table_semantics`);
229// no `__` prefix is needed since `greptime_private` already scopes them.
230/// Computed entity registry: the node set of the observability graph, derived at
231/// read time from tables that declared `greptime.semantic.entity.*` identities.
232pub const SEMANTIC_ENTITIES_TABLE_NAME: &str = "semantic_entities";
233/// Computed relationship set: the edge set of the observability graph, derived at
234/// read time (`calls`/`runs_on`/... ) and unioned with the declared-edge table.
235pub const SEMANTIC_RELATIONSHIPS_TABLE_NAME: &str = "semantic_relationships";
236/// Physical table of hand-declared edges, unioned into the computed
237/// `semantic_relationships`; see [`is_ddl_reserved_table`] for its lifecycle.
238pub const SEMANTIC_RELATIONSHIPS_DECLARED_TABLE_NAME: &str = "semantic_relationships_declared";
239
240/// Width of the window the graph derivation bins observations into. Sources
241/// synthesizing observations must land a row in every window they describe,
242/// so this is shared rather than restated per crate.
243pub const SEMANTIC_GRAPH_WINDOW_NANOS: i64 = 60 * 1_000_000_000;
244
245// Column names of the graph tables: `catalog` exposes these schemas and the
246// read-time plans in `operator` must project exactly them.
247pub const OBSERVED_AT_COLUMN: &str = "observed_at";
248pub const WINDOW_START_COLUMN: &str = "window_start";
249pub const WINDOW_END_COLUMN: &str = "window_end";
250pub const FRESH_UNTIL_COLUMN: &str = "fresh_until";
251pub const ENTITY_TYPE_COLUMN: &str = "entity_type";
252pub const ENTITY_ID_COLUMN: &str = "entity_id";
253pub const ENTITY_ID_ATTRS_COLUMN: &str = "entity_id_attrs";
254pub const ENTITY_SCOPE_COLUMN: &str = "scope";
255pub const ENTITY_DESCRIPTIVE_COLUMN: &str = "descriptive";
256pub const SOURCE_TABLES_COLUMN: &str = "source_tables";
257pub const SRC_TYPE_COLUMN: &str = "src_type";
258pub const SRC_ID_COLUMN: &str = "src_id";
259pub const DST_TYPE_COLUMN: &str = "dst_type";
260pub const DST_ID_COLUMN: &str = "dst_id";
261pub const REL_TYPE_COLUMN: &str = "rel_type";
262pub const PROVENANCE_COLUMN: &str = "provenance";
263pub const CONFIDENCE_COLUMN: &str = "confidence";
264pub const REQUEST_COUNT_COLUMN: &str = "request_count";
265pub const UNMATCHED_COUNT_COLUMN: &str = "unmatched_count";
266pub const ERROR_COUNT_COLUMN: &str = "error_count";
267pub const DURATION_SUM_COLUMN: &str = "duration_sum";
268pub const DURATION_COUNT_COLUMN: &str = "duration_count";
269pub const DURATION_MAX_COLUMN: &str = "duration_max";
270pub const EDGE_ATTRIBUTES_COLUMN: &str = "attributes";
271// Declared-edge table only.
272pub const VALID_FROM_COLUMN: &str = "valid_from";
273pub const VALID_UNTIL_COLUMN: &str = "valid_until";
274pub const GENERATION_ID_COLUMN: &str = "generation_id";
275// ---- End of entity relationship graph tables ----