Skip to content

Commit

Permalink
Merge pull request #1767 from ygf11/feat_toYYYYMMDDhhmmss_function
Browse files Browse the repository at this point in the history
Add toYYYYMMDDhhmmss function
  • Loading branch information
databend-bot authored Sep 9, 2021
2 parents 6b7f439 + ec2dab5 commit e1157d4
Show file tree
Hide file tree
Showing 8 changed files with 539 additions and 29 deletions.
7 changes: 7 additions & 0 deletions common/functions/src/scalars/dates/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ use common_exception::Result;

use super::now::NowFunction;
use super::RoundFunction;
use super::ToYYYYMMDDFunction;
use super::ToYYYYMMDDhhmmssFunction;
use super::ToYYYYMMFunction;
use super::TodayFunction;
use super::TomorrowFunction;
Expand All @@ -33,6 +35,11 @@ impl DateFunction {
map.insert("tomorrow".into(), TomorrowFunction::try_create);
map.insert("now".into(), NowFunction::try_create);
map.insert("toYYYYMM".into(), ToYYYYMMFunction::try_create);
map.insert("toYYYYMMDD".into(), ToYYYYMMDDFunction::try_create);
map.insert(
"toYYYYMMDDhhmmss".into(),
ToYYYYMMDDhhmmssFunction::try_create,
);

// rounders
{
Expand Down
293 changes: 293 additions & 0 deletions common/functions/src/scalars/dates/date_function_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@
// limitations under the License.

use common_arrow::arrow::array::UInt32Array;
use common_arrow::arrow::array::UInt64Array;
use common_datablocks::DataBlock;
use common_datavalues::prelude::*;
use common_exception::Result;

use crate::scalars::ToYYYYMMDDFunction;
use crate::scalars::ToYYYYMMDDhhmmssFunction;
use crate::scalars::ToYYYYMMFunction;

#[test]
Expand Down Expand Up @@ -247,3 +250,293 @@ fn test_toyyyymm_constant_function() -> Result<()> {

Ok(())
}

#[test]
fn test_toyyyymmdd_function() -> Result<()> {
// date16
let schema = DataSchemaRefExt::create(vec![DataField::new("a", DataType::Date16, false)]);
let block = DataBlock::create_by_array(schema.clone(), vec![Series::new(vec![0u16])]);

{
let col = ToYYYYMMDDFunction::try_create("a")?;
let columns = vec![DataColumnWithField::new(
block.try_column_by_name("a")?.clone(),
schema.field_with_name("a")?.clone(),
)];
let result = col.eval(&columns, block.num_rows())?;
assert_eq!(result.len(), 1);
assert_eq!(result.data_type(), DataType::UInt32);

let actual_ref = result.get_array_ref().unwrap();
let actual = actual_ref.as_any().downcast_ref::<UInt32Array>().unwrap();
let expected = UInt32Array::from_slice([19700101; 1]);
assert_eq!(actual, &expected);
}

// date32
let schema = DataSchemaRefExt::create(vec![DataField::new("a", DataType::Date32, false)]);
let block = DataBlock::create_by_array(schema.clone(), vec![Series::new(vec![0u32])]);

{
let col = ToYYYYMMDDFunction::try_create("a")?;
let columns = vec![DataColumnWithField::new(
block.try_column_by_name("a")?.clone(),
schema.field_with_name("a")?.clone(),
)];
let result = col.eval(&columns, block.num_rows())?;
assert_eq!(result.len(), 1);
assert_eq!(result.data_type(), DataType::UInt32);

let actual_ref = result.get_array_ref().unwrap();
let actual = actual_ref.as_any().downcast_ref::<UInt32Array>().unwrap();
let expected = UInt32Array::from_slice([19700101; 1]);

assert_eq!(actual, &expected);
}

// dateTime
// 2021-09-05 09:23:17 --- 1630833797
let schema =
DataSchemaRefExt::create(vec![DataField::new("a", DataType::DateTime32(None), false)]);
let block = DataBlock::create_by_array(schema.clone(), vec![Series::new(vec![1630833797u32])]);

{
let col = ToYYYYMMDDFunction::try_create("a")?;
let columns = vec![DataColumnWithField::new(
block.try_column_by_name("a")?.clone(),
schema.field_with_name("a")?.clone(),
)];
let result = col.eval(&columns, block.num_rows())?;
assert_eq!(result.len(), 1);
assert_eq!(result.data_type(), DataType::UInt32);

let actual_ref = result.get_array_ref().unwrap();
let actual = actual_ref.as_any().downcast_ref::<UInt32Array>().unwrap();
let expected = UInt32Array::from_slice([20210905; 1]);

assert_eq!(actual, &expected);
}

Ok(())
}

#[test]
fn test_toyyyymmdd_constant_function() -> Result<()> {
// date16
let schema = DataSchemaRefExt::create(vec![DataField::new("a", DataType::Date16, false)]);
let block = DataBlock::create(schema.clone(), vec![DataColumn::Constant(
DataValue::UInt16(Some(0u16)),
5,
)]);
{
let col = ToYYYYMMDDFunction::try_create("a")?;
let columns = vec![DataColumnWithField::new(
block.try_column_by_name("a")?.clone(),
schema.field_with_name("a")?.clone(),
)];
let result = col.eval(&columns, block.num_rows())?;
assert_eq!(result.len(), 5);
assert_eq!(result.data_type(), DataType::UInt32);

let actual_ref = result.get_array_ref().unwrap();
let actual = actual_ref.as_any().downcast_ref::<UInt32Array>().unwrap();
let expected = UInt32Array::from_slice([19700101; 5]);

assert_eq!(actual, &expected);
}

// date32
let schema = DataSchemaRefExt::create(vec![DataField::new("a", DataType::Date32, false)]);
let block = DataBlock::create(schema.clone(), vec![DataColumn::Constant(
DataValue::UInt32(Some(0u32)),
10,
)]);
{
let col = ToYYYYMMDDFunction::try_create("a")?;
let columns = vec![DataColumnWithField::new(
block.try_column_by_name("a")?.clone(),
schema.field_with_name("a")?.clone(),
)];
let result = col.eval(&columns, block.num_rows())?;
assert_eq!(result.len(), 10);
assert_eq!(result.data_type(), DataType::UInt32);

let actual_ref = result.get_array_ref().unwrap();
let actual = actual_ref.as_any().downcast_ref::<UInt32Array>().unwrap();
let expected = UInt32Array::from_slice([19700101; 10]);

assert_eq!(actual, &expected);
}

// datetime
// 2021-09-05 09:23:17 --- 1630833797
let schema =
DataSchemaRefExt::create(vec![DataField::new("a", DataType::DateTime32(None), false)]);
let block = DataBlock::create(schema.clone(), vec![DataColumn::Constant(
DataValue::UInt32(Some(1630833797u32)),
15,
)]);
{
let col = ToYYYYMMDDFunction::try_create("a")?;
let columns = vec![DataColumnWithField::new(
block.try_column_by_name("a")?.clone(),
schema.field_with_name("a")?.clone(),
)];
let result = col.eval(&columns, block.num_rows())?;
assert_eq!(result.len(), 15);
assert_eq!(result.data_type(), DataType::UInt32);

let actual_ref = result.get_array_ref().unwrap();
let actual = actual_ref.as_any().downcast_ref::<UInt32Array>().unwrap();
let expected = UInt32Array::from_slice([20210905; 15]);

assert_eq!(actual, &expected);
}

Ok(())
}

#[test]
fn test_toyyyymmddhhmmss_function() -> Result<()> {
// date16
let schema = DataSchemaRefExt::create(vec![DataField::new("a", DataType::Date16, false)]);
let block = DataBlock::create_by_array(schema.clone(), vec![Series::new(vec![0u16])]);

{
let col = ToYYYYMMDDhhmmssFunction::try_create("a")?;
let columns = vec![DataColumnWithField::new(
block.try_column_by_name("a")?.clone(),
schema.field_with_name("a")?.clone(),
)];
let result = col.eval(&columns, block.num_rows())?;
assert_eq!(result.len(), 1);
assert_eq!(result.data_type(), DataType::UInt64);

let actual_ref = result.get_array_ref().unwrap();
let actual = actual_ref.as_any().downcast_ref::<UInt64Array>().unwrap();
let expected = UInt64Array::from_slice([19700101000000; 1]);
assert_eq!(actual, &expected);
}

// date32
let schema = DataSchemaRefExt::create(vec![DataField::new("a", DataType::Date32, false)]);
let block = DataBlock::create_by_array(schema.clone(), vec![Series::new(vec![0u32])]);

{
let col = ToYYYYMMDDhhmmssFunction::try_create("a")?;
let columns = vec![DataColumnWithField::new(
block.try_column_by_name("a")?.clone(),
schema.field_with_name("a")?.clone(),
)];
let result = col.eval(&columns, block.num_rows())?;
assert_eq!(result.len(), 1);
assert_eq!(result.data_type(), DataType::UInt64);

let actual_ref = result.get_array_ref().unwrap();
let actual = actual_ref.as_any().downcast_ref::<UInt64Array>().unwrap();
let expected = UInt64Array::from_slice([19700101000000; 1]);

assert_eq!(actual, &expected);
}

// dateTime
// 2021-09-05 09:23:17 --- 1630833797
let schema =
DataSchemaRefExt::create(vec![DataField::new("a", DataType::DateTime32(None), false)]);
let block = DataBlock::create_by_array(schema.clone(), vec![Series::new(vec![1630833797u32])]);

{
let col = ToYYYYMMDDhhmmssFunction::try_create("a")?;
let columns = vec![DataColumnWithField::new(
block.try_column_by_name("a")?.clone(),
schema.field_with_name("a")?.clone(),
)];
let result = col.eval(&columns, block.num_rows())?;
assert_eq!(result.len(), 1);
assert_eq!(result.data_type(), DataType::UInt64);

let actual_ref = result.get_array_ref().unwrap();
let actual = actual_ref.as_any().downcast_ref::<UInt64Array>().unwrap();
let expected = UInt64Array::from_slice([20210905092317; 1]);

assert_eq!(actual, &expected);
}

Ok(())
}

#[test]
fn test_toyyyymmhhmmss_constant_function() -> Result<()> {
// date16
let schema = DataSchemaRefExt::create(vec![DataField::new("a", DataType::Date16, false)]);
let block = DataBlock::create(schema.clone(), vec![DataColumn::Constant(
DataValue::UInt16(Some(0u16)),
5,
)]);
{
let col = ToYYYYMMDDhhmmssFunction::try_create("a")?;
let columns = vec![DataColumnWithField::new(
block.try_column_by_name("a")?.clone(),
schema.field_with_name("a")?.clone(),
)];
let result = col.eval(&columns, block.num_rows())?;
assert_eq!(result.len(), 5);
assert_eq!(result.data_type(), DataType::UInt64);

let actual_ref = result.get_array_ref().unwrap();
let actual = actual_ref.as_any().downcast_ref::<UInt64Array>().unwrap();
let expected = UInt64Array::from_slice([19700101000000; 5]);

assert_eq!(actual, &expected);
}

// date32
let schema = DataSchemaRefExt::create(vec![DataField::new("a", DataType::Date32, false)]);
let block = DataBlock::create(schema.clone(), vec![DataColumn::Constant(
DataValue::UInt32(Some(0u32)),
10,
)]);
{
let col = ToYYYYMMDDhhmmssFunction::try_create("a")?;
let columns = vec![DataColumnWithField::new(
block.try_column_by_name("a")?.clone(),
schema.field_with_name("a")?.clone(),
)];
let result = col.eval(&columns, block.num_rows())?;
assert_eq!(result.len(), 10);
assert_eq!(result.data_type(), DataType::UInt64);

let actual_ref = result.get_array_ref().unwrap();
let actual = actual_ref.as_any().downcast_ref::<UInt64Array>().unwrap();
let expected = UInt64Array::from_slice([19700101000000; 10]);

assert_eq!(actual, &expected);
}

// datetime
// 2021-09-05 09:23:17 --- 1630833797
let schema =
DataSchemaRefExt::create(vec![DataField::new("a", DataType::DateTime32(None), false)]);
let block = DataBlock::create(schema.clone(), vec![DataColumn::Constant(
DataValue::UInt32(Some(1630833797u32)),
15,
)]);
{
let col = ToYYYYMMDDhhmmssFunction::try_create("a")?;
let columns = vec![DataColumnWithField::new(
block.try_column_by_name("a")?.clone(),
schema.field_with_name("a")?.clone(),
)];
let result = col.eval(&columns, block.num_rows())?;
assert_eq!(result.len(), 15);
assert_eq!(result.data_type(), DataType::UInt64);

let actual_ref = result.get_array_ref().unwrap();
let actual = actual_ref.as_any().downcast_ref::<UInt64Array>().unwrap();
let expected = UInt64Array::from_slice([20210905092317; 15]);

assert_eq!(actual, &expected);
}

Ok(())
}
2 changes: 2 additions & 0 deletions common/functions/src/scalars/dates/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ mod round_function;
mod simple_date;

pub use date::DateFunction;
pub use number_function::ToYYYYMMDDFunction;
pub use number_function::ToYYYYMMDDhhmmssFunction;
pub use number_function::ToYYYYMMFunction;
pub use round_function::RoundFunction;
pub use simple_date::TodayFunction;
Expand Down
Loading

0 comments on commit e1157d4

Please sign in to comment.