Skip to main content

store_api/
metric_engine_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
15//! Constants used in metric engine.
16
17use crate::storage::RegionGroup;
18
19/// region group value for data region inside a metric region
20pub const METRIC_DATA_REGION_GROUP: RegionGroup = 0;
21
22/// region group value for metadata region inside a metric region
23pub const METRIC_METADATA_REGION_GROUP: RegionGroup = 1;
24
25pub const METADATA_SCHEMA_TIMESTAMP_COLUMN_NAME: &str = "ts";
26pub const METADATA_SCHEMA_KEY_COLUMN_NAME: &str = "k";
27pub const METADATA_SCHEMA_VALUE_COLUMN_NAME: &str = "v";
28
29pub const METADATA_SCHEMA_TIMESTAMP_COLUMN_INDEX: usize = 0;
30pub const METADATA_SCHEMA_KEY_COLUMN_INDEX: usize = 1;
31pub const METADATA_SCHEMA_VALUE_COLUMN_INDEX: usize = 2;
32
33/// Column name of internal column `__metric` that stores the original metric name
34pub const DATA_SCHEMA_TABLE_ID_COLUMN_NAME: &str = "__table_id";
35pub const DATA_SCHEMA_TSID_COLUMN_NAME: &str = "__tsid";
36
37pub const METADATA_REGION_SUBDIR: &str = "metadata";
38pub const DATA_REGION_SUBDIR: &str = "data";
39
40pub const METRIC_ENGINE_NAME: &str = "metric";
41
42pub const FILE_ENGINE_NAME: &str = "file";
43
44pub const RELATIONAL_ENGINE_NAME: &str = "relational";
45
46/// Metadata key present in the `CREATE TABLE ... WITH ()` clause. This key is
47/// used to identify the table is a physical metric table. E.g.:
48/// ```sql
49/// CREATE TABLE physical_table (
50///     ...
51/// )
52/// ENGINE = metric
53/// WITH (
54///     physical_metric_table,
55/// );
56/// ```
57pub const PHYSICAL_TABLE_METADATA_KEY: &str = "physical_metric_table";
58
59/// Metadata key present in the `CREATE TABLE ... WITH ()` clause. This key is
60/// used to identify a logical table and associate it with a corresponding physical
61/// table . E.g.:
62/// ```sql
63/// CREATE TABLE logical_table (
64///     ...
65/// )
66/// ENGINE = metric
67/// WITH (
68///     on_physical_table = "physical_table",
69/// );
70/// ```
71/// And this key will be translated to corresponding physical **REGION** id in metasrv.
72pub const LOGICAL_TABLE_METADATA_KEY: &str = "on_physical_table";
73
74/// HashMap key to be used in the region server's extension response.
75/// Represent a list of column metadata that are added to physical table.
76pub const ALTER_PHYSICAL_EXTENSION_KEY: &str = "ALTER_PHYSICAL";
77
78/// HashMap key to be used in the region server's extension response.
79/// Represent the column metadata of a table.
80pub const TABLE_COLUMN_METADATA_EXTENSION_KEY: &str = "TABLE_COLUMN_METADATA";
81
82/// HashMap key to be used in the region server's extension response.
83/// Represent the manifest info of a region.
84pub const MANIFEST_INFO_EXTENSION_KEY: &str = "MANIFEST_INFO";
85
86/// Returns true if it's a internal column of the metric engine.
87pub fn is_metric_engine_internal_column(name: &str) -> bool {
88    name == DATA_SCHEMA_TABLE_ID_COLUMN_NAME || name == DATA_SCHEMA_TSID_COLUMN_NAME
89}
90
91/// Returns true if it's metric engine
92pub fn is_metric_engine(name: &str) -> bool {
93    name == METRIC_ENGINE_NAME
94}
95
96/// Option key for metric engine index type.
97/// Used to identify the primary key index type of the metric engine.
98/// ```sql
99/// CREATE TABLE table_name (
100///     ...
101/// )
102/// ENGINE = metric
103/// WITH (
104///     physical_metric_table = "",
105///     index.type = "inverted",
106///     index.inverted_index.segment_row_count = "256",
107/// );
108/// ```
109pub const METRIC_ENGINE_INDEX_TYPE_OPTION: &str = "index.type";
110
111/// Option key for the granularity of the skipping index in the metric engine.
112/// This key is used to specify the granularity of the primary key index (skipping index) in the metric engine.
113/// ```sql
114/// CREATE TABLE table_name (
115///     ...
116/// )
117/// ENGINE = metric
118/// WITH (
119///     physical_metric_table = "",
120///     index.type = "skipping",
121///     index.granularity = "102400",
122///     index.false_positive_rate = "0.01",
123/// );
124/// ```
125pub const METRIC_ENGINE_INDEX_SKIPPING_INDEX_GRANULARITY_OPTION: &str = "index.granularity";
126pub const METRIC_ENGINE_INDEX_SKIPPING_INDEX_FALSE_POSITIVE_RATE_OPTION: &str =
127    "index.false_positive_rate";
128
129/// Default granularity for the skipping index in the metric engine.
130pub const METRIC_ENGINE_INDEX_SKIPPING_INDEX_GRANULARITY_OPTION_DEFAULT: u32 = 102400;
131
132/// Default false positive rate for the skipping index in the metric engine.
133pub const METRIC_ENGINE_INDEX_SKIPPING_INDEX_FALSE_POSITIVE_RATE_OPTION_DEFAULT: f64 = 0.01;
134
135/// Top-level region option key for primary key encoding. Metric-engine internal use.
136pub const PRIMARY_KEY_ENCODING: &str = "primary_key_encoding";
137
138/// Deprecated. Kept for backward compatibility: parsing still accepts this key
139/// and migrates the value to [`PRIMARY_KEY_ENCODING`].
140pub const MEMTABLE_PARTITION_TREE_PRIMARY_KEY_ENCODING: &str =
141    "memtable.partition_tree.primary_key_encoding";
142
143/// Returns true if the `key` is a valid option key for the metric engine.
144pub fn is_metric_engine_option_key(key: &str) -> bool {
145    [
146        PHYSICAL_TABLE_METADATA_KEY,
147        LOGICAL_TABLE_METADATA_KEY,
148        METRIC_ENGINE_INDEX_TYPE_OPTION,
149        METRIC_ENGINE_INDEX_SKIPPING_INDEX_GRANULARITY_OPTION,
150        METRIC_ENGINE_INDEX_SKIPPING_INDEX_FALSE_POSITIVE_RATE_OPTION,
151        // Only allow setting primary key encoding by metric engine.
152        PRIMARY_KEY_ENCODING,
153        MEMTABLE_PARTITION_TREE_PRIMARY_KEY_ENCODING,
154    ]
155    .contains(&key)
156}