operator/req_convert/insert/timestamps.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 arrow::record_batch::RecordBatch;
16use datatypes::timestamp::append_timestamps;
17use snafu::OptionExt;
18
19use crate::error::{self, Result};
20
21/// Extracts non-null timestamps in the source column's native time unit.
22pub fn extract_timestamps(rb: &RecordBatch, timestamp_index_name: &str) -> Result<Vec<i64>> {
23 let ts_col = rb
24 .column_by_name(timestamp_index_name)
25 .context(error::ColumnNotFoundSnafu {
26 msg: timestamp_index_name,
27 })?;
28 if rb.num_rows() == 0 {
29 return Ok(vec![]);
30 }
31 let mut timestamps = Vec::with_capacity(rb.num_rows());
32 append_timestamps(ts_col, &mut timestamps).with_context(|| {
33 error::InvalidTimeIndexTypeSnafu {
34 ty: ts_col.data_type().clone(),
35 }
36 })?;
37 Ok(timestamps)
38}