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
// Copyright 2023 Greptime Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::collections::HashMap;

use api::v1::value::ValueData;
use api::v1::{
    ColumnDataType, ColumnSchema, Row, RowInsertRequest, RowInsertRequests, Rows, SemanticType,
    Value,
};
use common_grpc::precision::Precision;
use common_time::timestamp::TimeUnit;
use common_time::timestamp::TimeUnit::Nanosecond;
use common_time::Timestamp;
use snafu::{ensure, OptionExt, ResultExt};

use crate::error::{
    IncompatibleSchemaSnafu, Result, RowWriterSnafu, TimePrecisionSnafu, TimestampOverflowSnafu,
};

/// The intermediate data structure for building the write request.
/// It constructs the `schema` and `rows` as all input data row
/// parsing is completed.
pub struct TableData {
    schema: Vec<ColumnSchema>,
    rows: Vec<Row>,
    column_indexes: HashMap<String, usize>,
}

impl TableData {
    pub fn new(num_columns: usize, num_rows: usize) -> Self {
        Self {
            schema: Vec::with_capacity(num_columns),
            rows: Vec::with_capacity(num_rows),
            column_indexes: HashMap::with_capacity(num_columns),
        }
    }

    #[inline]
    pub fn num_columns(&self) -> usize {
        self.schema.len()
    }

    #[inline]
    pub fn num_rows(&self) -> usize {
        self.rows.len()
    }

    #[inline]
    pub fn alloc_one_row(&self) -> Vec<Value> {
        vec![Value { value_data: None }; self.num_columns()]
    }

    #[inline]
    pub fn add_row(&mut self, values: Vec<Value>) {
        self.rows.push(Row { values })
    }

    #[allow(dead_code)]
    pub fn columns(&self) -> &Vec<ColumnSchema> {
        &self.schema
    }

    pub fn into_schema_and_rows(self) -> (Vec<ColumnSchema>, Vec<Row>) {
        (self.schema, self.rows)
    }
}

pub struct MultiTableData {
    table_data_map: HashMap<String, TableData>,
}

impl Default for MultiTableData {
    fn default() -> Self {
        Self::new()
    }
}

impl MultiTableData {
    pub fn new() -> Self {
        Self {
            table_data_map: HashMap::new(),
        }
    }

    pub fn get_or_default_table_data(
        &mut self,
        table_name: impl ToString,
        num_columns: usize,
        num_rows: usize,
    ) -> &mut TableData {
        self.table_data_map
            .entry(table_name.to_string())
            .or_insert_with(|| TableData::new(num_columns, num_rows))
    }

    pub fn add_table_data(&mut self, table_name: impl ToString, table_data: TableData) {
        self.table_data_map
            .insert(table_name.to_string(), table_data);
    }

    #[allow(dead_code)]
    pub fn num_tables(&self) -> usize {
        self.table_data_map.len()
    }

    /// Returns the request and number of rows in it.
    pub fn into_row_insert_requests(self) -> (RowInsertRequests, usize) {
        let mut total_rows = 0;
        let inserts = self
            .table_data_map
            .into_iter()
            .map(|(table_name, table_data)| {
                total_rows += table_data.num_rows();
                let num_columns = table_data.num_columns();
                let (schema, mut rows) = table_data.into_schema_and_rows();
                for row in &mut rows {
                    if num_columns > row.values.len() {
                        row.values.resize(num_columns, Value { value_data: None });
                    }
                }

                RowInsertRequest {
                    table_name,
                    rows: Some(Rows { schema, rows }),
                }
            })
            .collect::<Vec<_>>();
        let row_insert_requests = RowInsertRequests { inserts };

        (row_insert_requests, total_rows)
    }
}

/// Write data as tags into the table data.
pub fn write_tags(
    table_data: &mut TableData,
    tags: impl Iterator<Item = (String, String)>,
    one_row: &mut Vec<Value>,
) -> Result<()> {
    let ktv_iter = tags.map(|(k, v)| (k, ColumnDataType::String, ValueData::StringValue(v)));
    write_by_semantic_type(table_data, SemanticType::Tag, ktv_iter, one_row)
}

/// Write data as fields into the table data.
pub fn write_fields(
    table_data: &mut TableData,
    fields: impl Iterator<Item = (String, ColumnDataType, ValueData)>,
    one_row: &mut Vec<Value>,
) -> Result<()> {
    write_by_semantic_type(table_data, SemanticType::Field, fields, one_row)
}

/// Write data as a tag into the table data.
pub fn write_tag(
    table_data: &mut TableData,
    name: impl ToString,
    value: impl ToString,
    one_row: &mut Vec<Value>,
) -> Result<()> {
    write_by_semantic_type(
        table_data,
        SemanticType::Tag,
        std::iter::once((
            name.to_string(),
            ColumnDataType::String,
            ValueData::StringValue(value.to_string()),
        )),
        one_row,
    )
}

/// Write float64 data as a field into the table data.
pub fn write_f64(
    table_data: &mut TableData,
    name: impl ToString,
    value: f64,
    one_row: &mut Vec<Value>,
) -> Result<()> {
    write_fields(
        table_data,
        std::iter::once((
            name.to_string(),
            ColumnDataType::Float64,
            ValueData::F64Value(value),
        )),
        one_row,
    )
}

fn write_by_semantic_type(
    table_data: &mut TableData,
    semantic_type: SemanticType,
    ktv_iter: impl Iterator<Item = (String, ColumnDataType, ValueData)>,
    one_row: &mut Vec<Value>,
) -> Result<()> {
    let TableData {
        schema,
        column_indexes,
        ..
    } = table_data;

    for (name, datatype, value) in ktv_iter {
        let index = column_indexes.get(&name);
        if let Some(index) = index {
            check_schema(datatype, semantic_type, &schema[*index])?;
            one_row[*index].value_data = Some(value);
        } else {
            let index = schema.len();
            schema.push(ColumnSchema {
                column_name: name.clone(),
                datatype: datatype as i32,
                semantic_type: semantic_type as i32,
                ..Default::default()
            });
            column_indexes.insert(name, index);
            one_row.push(Value {
                value_data: Some(value),
            });
        }
    }

    Ok(())
}

/// Write timestamp data as milliseconds into the table data.
pub fn write_ts_to_millis(
    table_data: &mut TableData,
    name: impl ToString,
    ts: Option<i64>,
    precision: Precision,
    one_row: &mut Vec<Value>,
) -> Result<()> {
    write_ts_to(
        table_data,
        name,
        ts,
        precision,
        TimestampType::Millis,
        one_row,
    )
}

/// Write timestamp data as nanoseconds into the table data.
pub fn write_ts_to_nanos(
    table_data: &mut TableData,
    name: impl ToString,
    ts: Option<i64>,
    precision: Precision,
    one_row: &mut Vec<Value>,
) -> Result<()> {
    write_ts_to(
        table_data,
        name,
        ts,
        precision,
        TimestampType::Nanos,
        one_row,
    )
}

enum TimestampType {
    Millis,
    Nanos,
}

fn write_ts_to(
    table_data: &mut TableData,
    name: impl ToString,
    ts: Option<i64>,
    precision: Precision,
    ts_type: TimestampType,
    one_row: &mut Vec<Value>,
) -> Result<()> {
    let TableData {
        schema,
        column_indexes,
        ..
    } = table_data;
    let name = name.to_string();

    let ts = match ts {
        Some(timestamp) => match ts_type {
            TimestampType::Millis => precision.to_millis(timestamp),
            TimestampType::Nanos => precision.to_nanos(timestamp),
        }
        .with_context(|| TimestampOverflowSnafu {
            error: format!(
                "timestamp {} overflow with precision {}",
                timestamp, precision
            ),
        })?,
        None => {
            let timestamp = Timestamp::current_time(Nanosecond);
            let unit: TimeUnit = precision.try_into().context(RowWriterSnafu)?;
            let timestamp = timestamp
                .convert_to(unit)
                .with_context(|| TimePrecisionSnafu {
                    name: precision.to_string(),
                })?
                .into();
            match ts_type {
                TimestampType::Millis => precision.to_millis(timestamp),
                TimestampType::Nanos => precision.to_nanos(timestamp),
            }
            .with_context(|| TimestampOverflowSnafu {
                error: format!(
                    "timestamp {} overflow with precision {}",
                    timestamp, precision
                ),
            })?
        }
    };

    let (datatype, ts) = match ts_type {
        TimestampType::Millis => (
            ColumnDataType::TimestampMillisecond,
            ValueData::TimestampMillisecondValue(ts),
        ),
        TimestampType::Nanos => (
            ColumnDataType::TimestampNanosecond,
            ValueData::TimestampNanosecondValue(ts),
        ),
    };

    let index = column_indexes.get(&name);
    if let Some(index) = index {
        check_schema(datatype, SemanticType::Timestamp, &schema[*index])?;
        one_row[*index].value_data = Some(ts);
    } else {
        let index = schema.len();
        schema.push(ColumnSchema {
            column_name: name.clone(),
            datatype: datatype as i32,
            semantic_type: SemanticType::Timestamp as i32,
            ..Default::default()
        });
        column_indexes.insert(name, index);
        one_row.push(ts.into())
    }

    Ok(())
}

fn check_schema(
    datatype: ColumnDataType,
    semantic_type: SemanticType,
    schema: &ColumnSchema,
) -> Result<()> {
    ensure!(
        schema.datatype == datatype as i32,
        IncompatibleSchemaSnafu {
            column_name: &schema.column_name,
            datatype: "datatype",
            expected: schema.datatype,
            actual: datatype as i32,
        }
    );

    ensure!(
        schema.semantic_type == semantic_type as i32,
        IncompatibleSchemaSnafu {
            column_name: &schema.column_name,
            datatype: "semantic_type",
            expected: schema.semantic_type,
            actual: semantic_type as i32,
        }
    );

    Ok(())
}