Skip to content

Commit

Permalink
feat: add toYYYYMMDDhhmmss doc
Browse files Browse the repository at this point in the history
  • Loading branch information
ygf11 committed Sep 8, 2021
1 parent 8860d38 commit 0930c10
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 15 deletions.
7 changes: 5 additions & 2 deletions common/functions/src/scalars/dates/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use common_exception::Result;

use super::now::NowFunction;
use super::RoundFunction;
use super::ToYYYYMMFunction;
use super::ToYYYYMMDDhhmmssFunction;
use super::ToYYYYMMFunction;
use super::TodayFunction;
use super::TomorrowFunction;
use super::YesterdayFunction;
Expand All @@ -34,7 +34,10 @@ 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("ToYYYYMMDDhhmmss".into(), ToYYYYMMDDhhmmssFunction::try_create);
map.insert(
"toYYYYMMDDhhmmss".into(),
ToYYYYMMDDhhmmssFunction::try_create,
);

// rounders
{
Expand Down
6 changes: 3 additions & 3 deletions common/functions/src/scalars/dates/date_function_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use common_datablocks::DataBlock;
use common_datavalues::prelude::*;
use common_exception::Result;

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

#[test]
fn test_toyyyymm_date16_function() -> Result<()> {
Expand Down Expand Up @@ -293,7 +293,7 @@ fn test_toyyyymmddhhmmss_function() -> Result<()> {
assert_eq!(actual, &expected);
}

// dateTime
// dateTime
// 2021-09-05 09:23:17 --- 1630833797
let schema =
DataSchemaRefExt::create(vec![DataField::new("a", DataType::DateTime32(None), false)]);
Expand Down Expand Up @@ -393,4 +393,4 @@ fn test_toyyyymmhhmmss_constant_function() -> Result<()> {
}

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

pub use date::DateFunction;
pub use number_function::ToYYYYMMFunction;
pub use number_function::ToYYYYMMDDhhmmssFunction;
pub use number_function::ToYYYYMMFunction;
pub use round_function::RoundFunction;
pub use simple_date::TodayFunction;
pub use simple_date::TomorrowFunction;
Expand Down
21 changes: 12 additions & 9 deletions common/functions/src/scalars/dates/number_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub struct NumberFunction<T, R> {
}

pub trait NumberResultFunction<R> {
fn return_type() -> Result<DataType>;
fn to_number(_value: DateTime<Utc>) -> R;
fn to_constant_value(_value: DateTime<Utc>) -> DataValue;
}
Expand All @@ -42,6 +43,9 @@ pub trait NumberResultFunction<R> {
pub struct ToYYYYMM;

impl NumberResultFunction<u32> for ToYYYYMM {
fn return_type() -> Result<DataType> {
Ok(DataType::UInt32)
}
fn to_number(value: DateTime<Utc>) -> u32 {
value.year() as u32 * 100 + value.month()
}
Expand All @@ -55,6 +59,10 @@ impl NumberResultFunction<u32> for ToYYYYMM {
pub struct ToYYYYMMDDhhmmss;

impl NumberResultFunction<u64> for ToYYYYMMDDhhmmss {
fn return_type() -> Result<DataType> {
Ok(DataType::UInt64)
}

fn to_number(value: DateTime<Utc>) -> u64 {
value.year() as u64 * 10000000000
+ value.month() as u64 * 100000000
Expand Down Expand Up @@ -95,7 +103,7 @@ where
}

fn return_type(&self, _args: &[DataType]) -> Result<DataType> {
Ok(DataType::UInt32)
T::return_type()
}

fn num_arguments(&self) -> usize {
Expand All @@ -112,9 +120,7 @@ where
DataType::Date16 => {
if let DataColumn::Constant(v, _) = columns[0].column() {
let date_time = Utc.timestamp(v.as_u64().unwrap() as i64 * 24 * 3600, 0_u32);
// let constant_result = Some(T::execute(date_time));
let constant_result = T::to_constant_value(date_time);
// Ok(DataColumn::Constant(DataValue::UInt32(constant_result), input_rows))
Ok(DataColumn::Constant(constant_result, input_rows))
}else {
let result: DFPrimitiveArray<R> = columns[0].column()
Expand All @@ -131,8 +137,6 @@ where
DataType::Date32 => {
if let DataColumn::Constant(v, _) = columns[0].column() {
let date_time = Utc.timestamp(v.as_u64().unwrap() as i64 * 24 * 3600, 0_u32);
// let constant_result = Some(T::execute(date_time));
// Ok(DataColumn::Constant(DataValue::UInt32(constant_result), input_rows))
let constant_result = T::to_constant_value(date_time);
Ok(DataColumn::Constant(constant_result, input_rows))
}else {
Expand All @@ -150,8 +154,6 @@ where
DataType::DateTime32(_) => {
if let DataColumn::Constant(v, _) = columns[0].column() {
let date_time = Utc.timestamp(v.as_u64().unwrap() as i64, 0_u32);
// let constant_result = Some(T::execute(date_time));
// Ok(DataColumn::Constant(DataValue::UInt32(constant_result), input_rows))
let constant_result = T::to_constant_value(date_time);
Ok(DataColumn::Constant(constant_result, input_rows))
}else {
Expand All @@ -167,8 +169,9 @@ where
}
},
other => Result::Err(ErrorCode::IllegalDataType(format!(
"Illegal type {:?} of argument of function toYYYYMM.Should be a date16/data32 or a dateTime32",
other))),
"Illegal type {:?} of argument of function {}.Should be a date16/data32 or a dateTime32",
other,
self.name()))),
}?;
Ok(number_array)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
---
id: datetime-toyyyymmddhhmmss
title: toYYYYMMDDhhmmss
---

Converts a date or date with time to a UInt64 number containing the year and month number (YYYY * 10000000000 + MM * 100000000 + DD * 1000000 + hh * 10000 + mm * 100 + ss).
## Syntax

```sql
toYYYYMMDDhhmmss(expr)
```

## Return Type

UInt64, returns in `YYYYMMDDhhmmss` format.

## Examples

```
mysql> select toDate(18875);
+---------------+
| toDate(18875) |
+---------------+
| 2021-09-05 |
+---------------+
mysql> select toYYYYMMDDhhmmss(toDate(18875));
+---------------------------------+
| toYYYYMMDDhhmmss(toDate(18875)) |
+---------------------------------+
| 20210905000000 |
+---------------------------------+
mysql> select toTypeName(toYYYYMMDDhhmmss(toDate(18875)));
+---------------------------------------------+
| toTypeName(toYYYYMMDDhhmmss(toDate(18875))) |
+---------------------------------------------+
| UInt64 |
+---------------------------------------------+
mysql> select toDateTime(1630833797);
+------------------------+
| toDateTime(1630833797) |
+------------------------+
| 2021-09-05 09:23:17 |
+------------------------+
mysql> select toYYYYMMDDhhmmss(toDateTime(1630833797));
+------------------------------------------+
| toYYYYMMDDhhmmss(toDateTime(1630833797)) |
+------------------------------------------+
| 20210905092317 |
+------------------------------------------+
mysql> select toTypeName(toYYYYMMDDhhmmss(toDateTime(1630833797)));
+------------------------------------------------------+
| toTypeName(toYYYYMMDDhhmmss(toDateTime(1630833797))) |
+------------------------------------------------------+
| UInt64 |
+------------------------------------------------------+
```

0 comments on commit 0930c10

Please sign in to comment.