api/v1/helper.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 greptime_proto::v1::value::ValueData;
16use greptime_proto::v1::{ColumnDataType, ColumnSchema, Row, SemanticType, Value};
17
18/// Create a time index [ColumnSchema] with column's name and datatype.
19/// Other fields are left default.
20/// Useful when you just want to create a simple [ColumnSchema] without providing much struct fields.
21pub fn time_index_column_schema(name: &str, datatype: ColumnDataType) -> ColumnSchema {
22 ColumnSchema {
23 column_name: name.to_string(),
24 datatype: datatype as i32,
25 semantic_type: SemanticType::Timestamp as i32,
26 ..Default::default()
27 }
28}
29
30/// Create a tag [ColumnSchema] with column's name and datatype.
31/// Other fields are left default.
32/// Useful when you just want to create a simple [ColumnSchema] without providing much struct fields.
33pub fn tag_column_schema(name: &str, datatype: ColumnDataType) -> ColumnSchema {
34 ColumnSchema {
35 column_name: name.to_string(),
36 datatype: datatype as i32,
37 semantic_type: SemanticType::Tag as i32,
38 ..Default::default()
39 }
40}
41
42/// Create a field [ColumnSchema] with column's name and datatype.
43/// Other fields are left default.
44/// Useful when you just want to create a simple [ColumnSchema] without providing much struct fields.
45pub fn field_column_schema(name: &str, datatype: ColumnDataType) -> ColumnSchema {
46 ColumnSchema {
47 column_name: name.to_string(),
48 datatype: datatype as i32,
49 semantic_type: SemanticType::Field as i32,
50 ..Default::default()
51 }
52}
53
54/// Create a [Row] from [ValueData]s.
55/// Useful when you don't want to write much verbose codes.
56pub fn row(values: Vec<ValueData>) -> Row {
57 Row {
58 values: values
59 .into_iter()
60 .map(|x| Value {
61 value_data: Some(x),
62 })
63 .collect::<Vec<_>>(),
64 }
65}