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(query): migrate real time functions to v2. #8259

Merged
merged 2 commits into from
Oct 17, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions docs/doc/30-reference/20-functions/30-datetime-functions/now.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ NOW()

## Return Type

Datetime object, returns date and time in “YYYY-MM-DD hh:mm:ss” format.
Datetime object, returns date and time in “YYYY-MM-DD hh:mm:ss.ffffff” format.

## Examples

```sql
SELECT NOW();
+---------------------+
| NOW() |
+---------------------+
| 2021-09-03 01:26:07 |
+---------------------+
+----------------------------+
| NOW() |
+----------------------------+
| 2022-10-17 08:13:58.751449 |
+----------------------------+
```
9 changes: 9 additions & 0 deletions src/query/expression/src/date_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use chrono::Duration;
use chrono::NaiveDate;
use chrono::NaiveDateTime;
use chrono::TimeZone;
use chrono::Utc;
use chrono_tz::Tz;
use num_traits::AsPrimitive;

Expand Down Expand Up @@ -161,3 +162,11 @@ impl AddTimesImpl {
check_timestamp(ts.wrapping_add(delta.as_() * factor * 1_000_000))
}
}

#[inline]
pub fn today_date() -> i32 {
let now = Utc::now();
NaiveDate::from_ymd(now.year(), now.month(), now.day())
.signed_duration_since(NaiveDate::from_ymd(1970, 1, 1))
.num_days() as i32
}
34 changes: 34 additions & 0 deletions src/query/functions-v2/src/scalars/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
// limitations under the License.

use chrono::Datelike;
use chrono::Utc;
use common_arrow::arrow::temporal_conversions::EPOCH_DAYS_FROM_CE;
use common_expression::date_helper::today_date;
use common_expression::date_helper::AddDaysImpl;
use common_expression::date_helper::AddMonthsImpl;
use common_expression::date_helper::AddTimesImpl;
Expand Down Expand Up @@ -41,13 +43,15 @@ use common_expression::vectorize_with_builder_1_arg;
use common_expression::vectorize_with_builder_2_arg;
use common_expression::FunctionProperty;
use common_expression::FunctionRegistry;
use common_expression::Value;
use num_traits::AsPrimitive;

pub fn register(registry: &mut FunctionRegistry) {
register_cast_functions(registry);
register_try_cast_functions(registry);
register_add_functions(registry);
register_sub_functions(registry);
register_real_time_functions(registry);
}

fn number_domain_to_timestamp_domain<T: AsPrimitive<i64>>(
Expand Down Expand Up @@ -401,3 +405,33 @@ macro_rules! impl_register_arith_functions {

impl_register_arith_functions!(register_add_functions, "add", unsigned_ident);
impl_register_arith_functions!(register_sub_functions, "subtract", signed_ident);

fn register_real_time_functions(registry: &mut FunctionRegistry) {
registry.register_0_arg_core::<TimestampType, _, _>(
"now",
FunctionProperty::default(),
|| None,
|_| Ok(Value::Scalar(Utc::now().timestamp_micros())),
);

registry.register_0_arg_core::<DateType, _, _>(
"today",
FunctionProperty::default(),
|| None,
|_| Ok(Value::Scalar(today_date())),
);

registry.register_0_arg_core::<DateType, _, _>(
"yesterday",
FunctionProperty::default(),
|| None,
|_| Ok(Value::Scalar(today_date() - 1)),
);

registry.register_0_arg_core::<DateType, _, _>(
"tomorrow",
FunctionProperty::default(),
|| None,
|_| Ok(Value::Scalar(today_date() + 1)),
);
}