refactor: create_current_timestamp_vector by using VectorOp::cast (#2042)

* refactor using VectorOp cast

* add test case
This commit is contained in:
parkma99
2023-07-31 10:51:06 +08:00
committed by GitHub
parent f22b787fd9
commit fc6ebf58b4
2 changed files with 46 additions and 14 deletions

View File

@@ -13,7 +13,6 @@
// limitations under the License.
use std::fmt::{Display, Formatter};
use std::sync::Arc;
use common_time::util;
use serde::{Deserialize, Serialize};
@@ -22,7 +21,8 @@ use snafu::{ensure, ResultExt};
use crate::data_type::{ConcreteDataType, DataType};
use crate::error::{self, Result};
use crate::value::Value;
use crate::vectors::{Int64Vector, TimestampMillisecondVector, VectorRef};
use crate::vectors::operations::VectorOp;
use crate::vectors::{TimestampMillisecondVector, VectorRef};
const CURRENT_TIMESTAMP: &str = "current_timestamp()";
@@ -162,24 +162,23 @@ fn create_current_timestamp_vector(
data_type: &ConcreteDataType,
num_rows: usize,
) -> Result<VectorRef> {
// FIXME(yingwen): We should implements cast in VectorOp so we could cast the millisecond vector
// to other data type and avoid this match.
match data_type {
ConcreteDataType::Timestamp(_) => Ok(Arc::new(TimestampMillisecondVector::from_values(
std::iter::repeat(util::current_time_millis()).take(num_rows),
))),
ConcreteDataType::Int64(_) => Ok(Arc::new(Int64Vector::from_values(
std::iter::repeat(util::current_time_millis()).take(num_rows),
))),
_ => error::DefaultValueTypeSnafu {
let current_timestamp_vector = TimestampMillisecondVector::from_values(
std::iter::repeat(util::current_time_millis()).take(num_rows),
);
if data_type.is_timestamp_compatible() {
current_timestamp_vector.cast(data_type)
} else {
error::DefaultValueTypeSnafu {
reason: format!("Not support to assign current timestamp to {data_type:?} type",),
}
.fail(),
.fail()
}
}
#[cfg(test)]
mod tests {
use std::sync::Arc;
use super::*;
use crate::error::Error;
use crate::vectors::Int32Vector;
@@ -272,6 +271,39 @@ mod tests {
v.get(0)
);
let data_type = ConcreteDataType::timestamp_second_datatype();
let v = constraint
.create_default_vector(&data_type, false, 4)
.unwrap();
assert_eq!(4, v.len());
assert!(
matches!(v.get(0), Value::Timestamp(_)),
"v {:?} is not timestamp",
v.get(0)
);
let data_type = ConcreteDataType::timestamp_microsecond_datatype();
let v = constraint
.create_default_vector(&data_type, false, 4)
.unwrap();
assert_eq!(4, v.len());
assert!(
matches!(v.get(0), Value::Timestamp(_)),
"v {:?} is not timestamp",
v.get(0)
);
let data_type = ConcreteDataType::timestamp_nanosecond_datatype();
let v = constraint
.create_default_vector(&data_type, false, 4)
.unwrap();
assert_eq!(4, v.len());
assert!(
matches!(v.get(0), Value::Timestamp(_)),
"v {:?} is not timestamp",
v.get(0)
);
// Int64 type.
let data_type = ConcreteDataType::int64_datatype();
let v = constraint

View File

@@ -34,7 +34,7 @@ mod eq;
mod helper;
mod list;
mod null;
mod operations;
pub(crate) mod operations;
mod primitive;
mod string;
mod time;