Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(to_unixtime): add initial implementation #1186

Merged
merged 9 commits into from
Mar 21, 2023
9 changes: 8 additions & 1 deletion src/common/function/src/scalars/timestamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,18 @@
// 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::sync::Arc;
mod from_unixtime;
etolbakov marked this conversation as resolved.
Show resolved Hide resolved
mod to_unixtime;

use to_unixtime::ToUnixtimeFunction;

use crate::scalars::function_registry::FunctionRegistry;

pub(crate) struct TimestampFunction;

impl TimestampFunction {
pub fn register(_registry: &FunctionRegistry) {}
pub fn register(_registry: &FunctionRegistry) {
etolbakov marked this conversation as resolved.
Show resolved Hide resolved
registry.register(Arc::new(ToUnixtimeFunction::default()));
}
}
127 changes: 127 additions & 0 deletions src/common/function/src/scalars/timestamp/to_unixtime.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
// 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::fmt;
use std::sync::Arc;
use std::str::FromStr;

use common_query::error::{
ArrowComputeSnafu, IntoVectorSnafu, Result, TypeCastSnafu, UnsupportedInputDataTypeSnafu, self,
};
use common_query::prelude::{Signature, Volatility};
use arrow::compute::kernels::cast_utils::string_to_timestamp_nanos;
use common_time::timestamp::TimeUnit;
use datafusion::arrow;
use datatypes::arrow::compute;

use datatypes::vectors::{Int64Vector, StringVector, Vector};
use datatypes::arrow::datatypes::{DataType as ArrowDatatype, Int64Type};
use datatypes::data_type::DataType;
use datatypes::prelude::ConcreteDataType;
use datatypes::types::StringType;
use datatypes::vectors::{TimestampMillisecondVector, VectorRef, self};
use snafu::{ResultExt};

use crate::scalars::function::{Function, FunctionContext};


#[derive(Clone, Debug, Default)]
pub struct ToUnixtimeFunction;

const NAME: &str = "to_unixtime";


impl Function for ToUnixtimeFunction {
fn name(&self) -> &str {
NAME
}

fn return_type(&self, _input_types: &[ConcreteDataType]) -> Result<ConcreteDataType> {
Ok(ConcreteDataType::int64_datatype())
waynexia marked this conversation as resolved.
Show resolved Hide resolved
}

fn signature(&self) -> Signature {
Signature::exact(
vec![ConcreteDataType::String(StringType)],
Volatility::Immutable,
)
}

fn eval(&self, _func_ctx: FunctionContext, columns: &[VectorRef]) -> Result<VectorRef> {
match columns[0].data_type() {
waynexia marked this conversation as resolved.
Show resolved Hide resolved
ConcreteDataType::String(_) => {
let array = columns[0].to_arrow_array();
let _arrow_datatype = &self.return_type(&[]).unwrap().as_arrow_type();

let string_vector = StringVector::try_from_arrow_array(&array).unwrap();
let first = string_vector.get(0).to_string();
let first_timestamp = common_time::Timestamp::from_str(&first).unwrap();

let sec_mul = (TimeUnit::Second.factor() / first_timestamp.unit().factor()) as i64;
let result = first_timestamp.value().div_euclid(sec_mul);
println!("[result] {:?}", result); // 1677652502

UnsupportedInputDataTypeSnafu {
function: NAME,
datatypes: columns.iter().map(|c| c.data_type()).collect::<Vec<_>>(),
}.fail()
},
_ => UnsupportedInputDataTypeSnafu {
function: NAME,
datatypes: columns.iter().map(|c| c.data_type()).collect::<Vec<_>>(),
}
.fail(),
}
}
}

impl fmt::Display for ToUnixtimeFunction {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "TO_UNIXTIME")
}
}

#[cfg(test)]
mod tests {
use common_query::prelude::TypeSignature;
use datatypes::{prelude::ConcreteDataType, types::StringType};
use datatypes::vectors::{Int64Vector, StringVector};
use crate::scalars::Function;

use super::ToUnixtimeFunction;

use super::*;

#[test]
fn test_to_unixtime() {
let f = ToUnixtimeFunction::default();
assert_eq!("to_unixtime", f.name());
assert_eq!(
ConcreteDataType::int64_datatype(),
f.return_type(&[]).unwrap()
);

assert!(matches!(f.signature(),
Signature {
type_signature: TypeSignature::Exact(valid_types),
volatility: Volatility::Immutable
} if valid_types == vec![ConcreteDataType::String(StringType)]
));

let times = vec![Some("2023-03-01T06:35:02Z"), None, Some("2022-06-30T23:59:60Z")];
let args: Vec<VectorRef> = vec![Arc::new(StringVector::from(times.clone()))];
let vector = f.eval(FunctionContext::default(), &args).unwrap();
assert_eq!(3, vector.len());
}
}