From 6dbe21373565c8d79c134ab5f7de6e6c0745a6af Mon Sep 17 00:00:00 2001 From: RinChanNOWWW Date: Thu, 13 Oct 2022 20:39:59 +0800 Subject: [PATCH 1/8] Implement arithmetic functions for `Date` and `Timestamp`. --- src/query/expression/src/date_converter.rs | 46 ----- src/query/expression/src/date_helper.rs | 163 ++++++++++++++++++ src/query/expression/src/display.rs | 2 +- src/query/expression/src/lib.rs | 3 +- src/query/expression/src/types/timestamp.rs | 26 ++- .../functions-v2/src/scalars/datetime.rs | 150 ++++++++++++++-- 6 files changed, 324 insertions(+), 66 deletions(-) delete mode 100644 src/query/expression/src/date_converter.rs create mode 100644 src/query/expression/src/date_helper.rs diff --git a/src/query/expression/src/date_converter.rs b/src/query/expression/src/date_converter.rs deleted file mode 100644 index 83d1cf899450..000000000000 --- a/src/query/expression/src/date_converter.rs +++ /dev/null @@ -1,46 +0,0 @@ -// Copyright 2022 Datafuse Labs. -// -// 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 chrono::Date; -use chrono::DateTime; -use chrono::Duration; -use chrono::TimeZone; -use chrono_tz::Tz; -use num_traits::AsPrimitive; - -pub trait DateConverter { - fn to_date(&self, tz: &Tz) -> Date; - fn to_timestamp(&self, tz: &Tz) -> DateTime; -} - -impl DateConverter for T -where T: AsPrimitive -{ - fn to_date(&self, tz: &Tz) -> Date { - let mut dt = tz.ymd(1970, 1, 1); - dt = dt.checked_add_signed(Duration::days(self.as_())).unwrap(); - dt - } - - fn to_timestamp(&self, tz: &Tz) -> DateTime { - // Can't use `tz.timestamp_nanos(self.as_() * 1000)` directly, is may cause multiply with overflow. - let micros = self.as_(); - let (mut secs, mut nanos) = (micros / 1_000_000, (micros % 1_000_000) * 1_000); - if nanos < 0 { - secs -= 1; - nanos += 1_000_000_000; - } - tz.timestamp_opt(secs, nanos as u32).unwrap() - } -} diff --git a/src/query/expression/src/date_helper.rs b/src/query/expression/src/date_helper.rs new file mode 100644 index 000000000000..6ddcc59e7fa0 --- /dev/null +++ b/src/query/expression/src/date_helper.rs @@ -0,0 +1,163 @@ +// Copyright 2022 Datafuse Labs. +// +// 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 chrono::Date; +use chrono::DateTime; +use chrono::Datelike; +use chrono::Duration; +use chrono::NaiveDate; +use chrono::NaiveDateTime; +use chrono::TimeZone; +use chrono_tz::Tz; +use num_traits::AsPrimitive; + +use crate::types::date::check_date; +use crate::types::timestamp::check_timestamp; + +pub trait DateConverter { + fn to_date(&self, tz: &Tz) -> Date; + fn to_timestamp(&self, tz: &Tz) -> DateTime; +} + +impl DateConverter for T +where T: AsPrimitive +{ + fn to_date(&self, tz: &Tz) -> Date { + let mut dt = tz.ymd(1970, 1, 1); + dt = dt.checked_add_signed(Duration::days(self.as_())).unwrap(); + dt + } + + fn to_timestamp(&self, tz: &Tz) -> DateTime { + // Can't use `tz.timestamp_nanos(self.as_() * 1000)` directly, is may cause multiply with overflow. + let micros = self.as_(); + let (mut secs, mut nanos) = (micros / 1_000_000, (micros % 1_000_000) * 1_000); + if nanos < 0 { + secs -= 1; + nanos += 1_000_000_000; + } + tz.timestamp_opt(secs, nanos as u32).unwrap() + } +} + +// Timestamp arithmetic factors. +pub const FACTOR_HOUR: i64 = 3600; +pub const FACTOR_MINUTE: i64 = 60; +pub const FACTOR_SECOND: i64 = 1; + +fn add_years_base(year: i32, month: u32, day: u32, delta: i64) -> Result { + let new_year = year + delta as i32; + let mut new_day = day; + if std::intrinsics::unlikely(month == 2 && day == 29) { + new_day = last_day_of_year_month(new_year, month); + } + NaiveDate::from_ymd_opt(new_year, month, new_day).ok_or(format!( + "Overflow on date YMD {}-{}-{}.", + new_year, month, new_day + )) +} + +fn add_months_base(year: i32, month: u32, day: u32, delta: i64) -> Result { + let total_months = month as i64 + delta - 1; + let mut new_year = year + (total_months / 12) as i32; + let mut new_month0 = total_months % 12; + if new_month0 < 0 { + new_year -= 1; + new_month0 += 12; + } + + // Handle month last day overflow, "2020-2-29" + "1 year" should be "2021-2-28", or "1990-1-31" + "3 month" should be "1990-4-30". + let new_day = std::cmp::min::( + day, + last_day_of_year_month(new_year, (new_month0 + 1) as u32), + ); + + NaiveDate::from_ymd_opt(new_year, (new_month0 + 1) as u32, new_day).ok_or(format!( + "Overflow on date YMD {}-{}-{}.", + new_year, + new_month0 + 1, + new_day + )) +} + +// Get the last day of the year month, could be 28(non leap Feb), 29(leap year Feb), 30 or 31 +fn last_day_of_year_month(year: i32, month: u32) -> u32 { + let is_leap_year = NaiveDate::from_ymd_opt(year, 2, 29).is_some(); + if std::intrinsics::unlikely(month == 2 && is_leap_year) { + return 29; + } + let last_day_lookup = [0u32, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; + last_day_lookup[month as usize] +} + +macro_rules! impl_interval_year_month { + ($name: ident, $op: expr) => { + #[derive(Clone)] + pub struct $name; + + impl $name { + pub fn eval_date(date: i32, delta: impl AsPrimitive) -> Result { + let date = date.to_date(&Tz::UTC); + let new_date = $op(date.year(), date.month(), date.day(), delta.as_()); + new_date.and_then(|d| { + check_date( + d.signed_duration_since(NaiveDate::from_ymd(1970, 1, 1)) + .num_days(), + ) + }) + } + + pub fn eval_timestamp(ts: i64, delta: impl AsPrimitive) -> Result { + let ts = ts.to_timestamp(&Tz::UTC); + let new_ts = $op(ts.year(), ts.month(), ts.day(), delta.as_()); + new_ts.and_then(|t| { + check_timestamp(NaiveDateTime::new(t, ts.time()).timestamp_micros()) + }) + } + } + }; +} + +impl_interval_year_month!(AddYearsImpl, add_years_base); +impl_interval_year_month!(AddMonthsImpl, add_months_base); + +pub struct AddDaysImpl; + +impl AddDaysImpl { + pub fn eval_date(date: i32, delta: impl AsPrimitive) -> Result { + check_date((date as i64).wrapping_add(delta.as_())) + } + + pub fn eval_timestamp(date: i64, delta: impl AsPrimitive) -> Result { + check_timestamp((date as i64).wrapping_add(delta.as_() * 24 * 3600 * 1_000_000)) + } +} + +pub struct AddTimesImpl; + +impl AddTimesImpl { + pub fn eval_date(date: i32, delta: impl AsPrimitive, factor: i64) -> Result { + check_date( + (date as i64 * 3600 * 24 * 1_000_000).wrapping_add(delta.as_() * factor * 1_000_000), + ) + } + + pub fn eval_timestamp( + ts: i64, + delta: impl AsPrimitive, + factor: i64, + ) -> Result { + check_timestamp(ts.wrapping_add(delta.as_() * factor * 1_000_000)) + } +} diff --git a/src/query/expression/src/display.rs b/src/query/expression/src/display.rs index 1feb6c09d7ab..20e8b8decff6 100755 --- a/src/query/expression/src/display.rs +++ b/src/query/expression/src/display.rs @@ -24,7 +24,7 @@ use rust_decimal::Decimal; use rust_decimal::RoundingStrategy; use crate::chunk::Chunk; -use crate::date_converter::DateConverter; +use crate::date_helper::DateConverter; use crate::expression::Expr; use crate::expression::Literal; use crate::expression::RawExpr; diff --git a/src/query/expression/src/lib.rs b/src/query/expression/src/lib.rs index 7f9fe58c376d..ef5c3938b24d 100755 --- a/src/query/expression/src/lib.rs +++ b/src/query/expression/src/lib.rs @@ -25,6 +25,7 @@ #![feature(const_mut_refs)] #![feature(generic_const_exprs)] #![allow(incomplete_features)] +#![feature(core_intrinsics)] #![feature(iter_order_by)] #[allow(dead_code)] @@ -33,7 +34,7 @@ mod chunk; mod column_from; pub mod converts; -mod date_converter; +pub mod date_helper; mod display; mod error; mod evaluator; diff --git a/src/query/expression/src/types/timestamp.rs b/src/query/expression/src/types/timestamp.rs index 35770cc86b4c..4fc715f4c7dd 100644 --- a/src/query/expression/src/types/timestamp.rs +++ b/src/query/expression/src/types/timestamp.rs @@ -22,7 +22,7 @@ use common_io::prelude::BufferReadExt; use common_io::prelude::BufferReader; use super::number::SimpleDomain; -use crate::date_converter::DateConverter; +use crate::date_helper::DateConverter; use crate::property::Domain; use crate::types::ArgType; use crate::types::DataType; @@ -49,19 +49,29 @@ pub const PRECISION_MICRO: u8 = 6; pub const PRECISION_MILLI: u8 = 3; pub const PRECISION_SEC: u8 = 0; -/// check timestamp and return precision and the base. +/// check when converting number to timestamp and return precision and the base. #[inline] -pub fn check_timestamp(micros: i64) -> Result { - let base = if (-31536000000..=31536000000).contains(µs) { +pub fn check_number_to_timestamp(n: i64) -> Result { + let base = if (-31536000000..=31536000000).contains(&n) { MICROS_IN_A_SEC - } else if (-31536000000000..=31536000000000).contains(µs) { + } else if (-31536000000000..=31536000000000).contains(&n) { MICROS_IN_A_MILLI - } else if (TIMESTAMP_MIN..=TIMESTAMP_MAX).contains(µs) { + } else if (TIMESTAMP_MIN..=TIMESTAMP_MAX).contains(&n) { MICROS_IN_A_MICRO } else { - return Err(format!("timestamp `{}` is out of range", micros)); + return Err(format!("timestamp `{}` is out of range", n)); }; - Ok(micros * base) + Ok(n * base) +} + +/// check the micros in timestamp value. +#[inline] +pub fn check_timestamp(micros: i64) -> Result { + if (TIMESTAMP_MIN..=TIMESTAMP_MAX).contains(µs) { + Ok(micros) + } else { + Err(format!("timestamp `{}` is out of range", micros)) + } } #[derive(Debug, Clone, PartialEq, Eq)] diff --git a/src/query/functions-v2/src/scalars/datetime.rs b/src/query/functions-v2/src/scalars/datetime.rs index a24fd6d0cdfe..85ad18412d8c 100644 --- a/src/query/functions-v2/src/scalars/datetime.rs +++ b/src/query/functions-v2/src/scalars/datetime.rs @@ -14,6 +14,13 @@ use chrono::Datelike; use common_arrow::arrow::temporal_conversions::EPOCH_DAYS_FROM_CE; +use common_expression::date_helper::AddDaysImpl; +use common_expression::date_helper::AddMonthsImpl; +use common_expression::date_helper::AddTimesImpl; +use common_expression::date_helper::AddYearsImpl; +use common_expression::date_helper::FACTOR_HOUR; +use common_expression::date_helper::FACTOR_MINUTE; +use common_expression::date_helper::FACTOR_SECOND; use common_expression::types::date::check_date; use common_expression::types::date::string_to_date; use common_expression::types::date::DATE_MAX; @@ -21,7 +28,7 @@ use common_expression::types::date::DATE_MIN; use common_expression::types::nullable::NullableDomain; use common_expression::types::number::Int64Type; use common_expression::types::number::SimpleDomain; -use common_expression::types::timestamp::check_timestamp; +use common_expression::types::timestamp::check_number_to_timestamp; use common_expression::types::timestamp::microseconds_to_days; use common_expression::types::timestamp::string_to_timestamp; use common_expression::types::timestamp::MICROS_IN_A_SEC; @@ -31,19 +38,27 @@ use common_expression::types::StringType; use common_expression::types::TimestampType; use common_expression::vectorize_1_arg; 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 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); +} + fn number_domain_to_timestamp_domain>( domain: &SimpleDomain, ) -> Option> { - let min = if let Ok(min) = check_timestamp(domain.min.as_()) { + let min = if let Ok(min) = check_number_to_timestamp(domain.min.as_()) { min } else { return None; }; - let max = if let Ok(max) = check_timestamp(domain.max.as_()) { + let max = if let Ok(max) = check_number_to_timestamp(domain.max.as_()) { max } else { return None; @@ -51,11 +66,6 @@ fn number_domain_to_timestamp_domain>( Some(SimpleDomain { min, max }) } -pub fn register(registry: &mut FunctionRegistry) { - register_cast_functions(registry); - register_try_cast_functions(registry); -} - fn register_cast_functions(registry: &mut FunctionRegistry) { registry.register_aliases("to_timestamp", &["to_datetime"]); @@ -80,7 +90,7 @@ fn register_cast_functions(registry: &mut FunctionRegistry) { FunctionProperty::default(), |domain| number_domain_to_timestamp_domain(domain), vectorize_with_builder_1_arg::(|val, output, _| { - output.push(check_timestamp(val)?); + output.push(check_number_to_timestamp(val)?); Ok(()) }), ); @@ -180,7 +190,7 @@ fn register_try_cast_functions(registry: &mut FunctionRegistry) { }) }, vectorize_1_arg::, NullableType>(|val, _| { - val.and_then(|v| check_timestamp(v).ok()) + val.and_then(|v| check_number_to_timestamp(v).ok()) }), ); @@ -238,3 +248,123 @@ fn register_try_cast_functions(registry: &mut FunctionRegistry) { }), ); } + +macro_rules! impl_register_arith_functions { + ($name: ident, $op: literal, $sign: expr) => { + fn $name(registry: &mut FunctionRegistry) { + registry.register_passthrough_nullable_2_arg::( + concat!($op, "_years"), + FunctionProperty::default(), + |_, _| None, + vectorize_with_builder_2_arg::( + |ts, delta, builder, _| { + builder.push(AddYearsImpl::eval_timestamp(ts, delta * $sign)?); + Ok(()) + }, + ), + ); + registry.register_passthrough_nullable_2_arg::( + concat!($op, "_years"), + FunctionProperty::default(), + |_, _| None, + vectorize_with_builder_2_arg::(|date, delta, builder, _| { + builder.push(AddYearsImpl::eval_date(date, delta * $sign)?); + Ok(()) + }), + ); + + registry.register_passthrough_nullable_2_arg::( + concat!($op, "_months"), + FunctionProperty::default(), + |_, _| None, + vectorize_with_builder_2_arg::( + |ts, delta, builder, _| { + builder.push(AddMonthsImpl::eval_timestamp(ts, delta * $sign)?); + Ok(()) + }, + ), + ); + registry.register_passthrough_nullable_2_arg::( + concat!($op, "_months"), + FunctionProperty::default(), + |_, _| None, + vectorize_with_builder_2_arg::(|date, delta, builder, _| { + builder.push(AddMonthsImpl::eval_date(date, delta * $sign)?); + Ok(()) + }), + ); + + registry.register_passthrough_nullable_2_arg::( + concat!($op, "_days"), + FunctionProperty::default(), + |_, _| None, + vectorize_with_builder_2_arg::( + |ts, delta, builder, _| { + builder.push(AddDaysImpl::eval_timestamp(ts, delta * $sign)?); + Ok(()) + }, + ), + ); + registry.register_passthrough_nullable_2_arg::( + concat!($op, "_days"), + FunctionProperty::default(), + |_, _| None, + vectorize_with_builder_2_arg::(|date, delta, builder, _| { + builder.push(AddDaysImpl::eval_date(date, delta * $sign)?); + Ok(()) + }), + ); + + registry.register_passthrough_nullable_2_arg::( + concat!($op, "_hours"), + FunctionProperty::default(), + |_, _| None, + vectorize_with_builder_2_arg::( + |ts, delta, builder, _| { + builder.push(AddTimesImpl::eval_timestamp( + ts, + delta * $sign, + FACTOR_HOUR, + )?); + Ok(()) + }, + ), + ); + + registry.register_passthrough_nullable_2_arg::( + concat!($op, "_minutes"), + FunctionProperty::default(), + |_, _| None, + vectorize_with_builder_2_arg::( + |ts, delta, builder, _| { + builder.push(AddTimesImpl::eval_timestamp( + ts, + delta * $sign, + FACTOR_MINUTE, + )?); + Ok(()) + }, + ), + ); + + registry.register_passthrough_nullable_2_arg::( + concat!($op, "_seconds"), + FunctionProperty::default(), + |_, _| None, + vectorize_with_builder_2_arg::( + |ts, delta, builder, _| { + builder.push(AddTimesImpl::eval_timestamp( + ts, + delta * $sign, + FACTOR_SECOND, + )?); + Ok(()) + }, + ), + ); + } + }; +} + +impl_register_arith_functions!(register_add_functions, "add", 1); +impl_register_arith_functions!(register_sub_functions, "substract", -1); From ed633bd48b75b73e5a3204f0b2ce8c70204c40d5 Mon Sep 17 00:00:00 2001 From: RinChanNOWWW Date: Fri, 14 Oct 2022 15:26:27 +0800 Subject: [PATCH 2/8] Add unit test cases and fix clippy. --- .../functions-v2/src/scalars/datetime.rs | 36 +- .../functions-v2/tests/it/scalars/datetime.rs | 222 +++++++ .../tests/it/scalars/testdata/datetime.txt | 592 ++++++++++++++++++ 3 files changed, 838 insertions(+), 12 deletions(-) diff --git a/src/query/functions-v2/src/scalars/datetime.rs b/src/query/functions-v2/src/scalars/datetime.rs index 85ad18412d8c..f215b432b4f1 100644 --- a/src/query/functions-v2/src/scalars/datetime.rs +++ b/src/query/functions-v2/src/scalars/datetime.rs @@ -249,8 +249,20 @@ fn register_try_cast_functions(registry: &mut FunctionRegistry) { ); } +macro_rules! signed_ident { + ($name: ident) => { + -$name + }; +} + +macro_rules! unsigned_ident { + ($name: ident) => { + $name + }; +} + macro_rules! impl_register_arith_functions { - ($name: ident, $op: literal, $sign: expr) => { + ($name: ident, $op: literal, $signed_wrapper: tt) => { fn $name(registry: &mut FunctionRegistry) { registry.register_passthrough_nullable_2_arg::( concat!($op, "_years"), @@ -258,7 +270,7 @@ macro_rules! impl_register_arith_functions { |_, _| None, vectorize_with_builder_2_arg::( |ts, delta, builder, _| { - builder.push(AddYearsImpl::eval_timestamp(ts, delta * $sign)?); + builder.push(AddYearsImpl::eval_timestamp(ts, $signed_wrapper!{delta})?); Ok(()) }, ), @@ -268,7 +280,7 @@ macro_rules! impl_register_arith_functions { FunctionProperty::default(), |_, _| None, vectorize_with_builder_2_arg::(|date, delta, builder, _| { - builder.push(AddYearsImpl::eval_date(date, delta * $sign)?); + builder.push(AddYearsImpl::eval_date(date, $signed_wrapper!{delta})?); Ok(()) }), ); @@ -279,7 +291,7 @@ macro_rules! impl_register_arith_functions { |_, _| None, vectorize_with_builder_2_arg::( |ts, delta, builder, _| { - builder.push(AddMonthsImpl::eval_timestamp(ts, delta * $sign)?); + builder.push(AddMonthsImpl::eval_timestamp(ts, $signed_wrapper!{delta})?); Ok(()) }, ), @@ -289,7 +301,7 @@ macro_rules! impl_register_arith_functions { FunctionProperty::default(), |_, _| None, vectorize_with_builder_2_arg::(|date, delta, builder, _| { - builder.push(AddMonthsImpl::eval_date(date, delta * $sign)?); + builder.push(AddMonthsImpl::eval_date(date, $signed_wrapper!{delta})?); Ok(()) }), ); @@ -300,7 +312,7 @@ macro_rules! impl_register_arith_functions { |_, _| None, vectorize_with_builder_2_arg::( |ts, delta, builder, _| { - builder.push(AddDaysImpl::eval_timestamp(ts, delta * $sign)?); + builder.push(AddDaysImpl::eval_timestamp(ts, $signed_wrapper!{delta})?); Ok(()) }, ), @@ -310,7 +322,7 @@ macro_rules! impl_register_arith_functions { FunctionProperty::default(), |_, _| None, vectorize_with_builder_2_arg::(|date, delta, builder, _| { - builder.push(AddDaysImpl::eval_date(date, delta * $sign)?); + builder.push(AddDaysImpl::eval_date(date, $signed_wrapper!{delta})?); Ok(()) }), ); @@ -323,7 +335,7 @@ macro_rules! impl_register_arith_functions { |ts, delta, builder, _| { builder.push(AddTimesImpl::eval_timestamp( ts, - delta * $sign, + $signed_wrapper!{delta}, FACTOR_HOUR, )?); Ok(()) @@ -339,7 +351,7 @@ macro_rules! impl_register_arith_functions { |ts, delta, builder, _| { builder.push(AddTimesImpl::eval_timestamp( ts, - delta * $sign, + $signed_wrapper!{delta}, FACTOR_MINUTE, )?); Ok(()) @@ -355,7 +367,7 @@ macro_rules! impl_register_arith_functions { |ts, delta, builder, _| { builder.push(AddTimesImpl::eval_timestamp( ts, - delta * $sign, + $signed_wrapper!{delta}, FACTOR_SECOND, )?); Ok(()) @@ -366,5 +378,5 @@ macro_rules! impl_register_arith_functions { }; } -impl_register_arith_functions!(register_add_functions, "add", 1); -impl_register_arith_functions!(register_sub_functions, "substract", -1); +impl_register_arith_functions!(register_add_functions, "add", unsigned_ident); +impl_register_arith_functions!(register_sub_functions, "substract", signed_ident); diff --git a/src/query/functions-v2/tests/it/scalars/datetime.rs b/src/query/functions-v2/tests/it/scalars/datetime.rs index 72b15e1e92af..ebb04cac2fcd 100644 --- a/src/query/functions-v2/tests/it/scalars/datetime.rs +++ b/src/query/functions-v2/tests/it/scalars/datetime.rs @@ -14,6 +14,8 @@ use std::io::Write; +use common_expression::from_date_data; +use common_expression::from_timestamp_data; use common_expression::types::DataType; use common_expression::types::NumberDataType; use common_expression::Column; @@ -30,6 +32,8 @@ fn test_datetime() { test_to_timestamp(file); test_to_datetime(file); test_to_date(file); + test_date_arithmetic_functions(file); + test_timestamp_arithmetic_functions(file); } fn test_to_timestamp(file: &mut impl Write) { @@ -99,3 +103,221 @@ fn test_to_date(file: &mut impl Write) { Column::from_data(vec![-354285, -100, 0, 100, 2932896]), )]); } + +fn test_date_arithmetic_functions(file: &mut impl Write) { + run_ast(file, "add_years(to_date(0), 10000)", &[]); // failed + run_ast(file, "add_years(to_date(0), 100)", &[]); + run_ast(file, "add_months(to_date(0), 100)", &[]); + run_ast(file, "add_days(to_date(0), 100)", &[]); + run_ast(file, "substract_years(to_date(0), 100)", &[]); + run_ast(file, "substract_months(to_date(0), 100)", &[]); + run_ast(file, "substract_days(to_date(0), 100)", &[]); + run_ast(file, "add_years(a, b)", &[ + ("a", DataType::Date, from_date_data(vec![-100, 0, 100])), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); + run_ast(file, "add_months(a, b)", &[ + ("a", DataType::Date, from_date_data(vec![-100, 0, 100])), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); + run_ast(file, "add_days(a, b)", &[ + ("a", DataType::Date, from_date_data(vec![-100, 0, 100])), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); + run_ast(file, "substract_years(a, b)", &[ + ("a", DataType::Date, from_date_data(vec![-100, 0, 100])), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); + run_ast(file, "substract_months(a, b)", &[ + ("a", DataType::Date, from_date_data(vec![-100, 0, 100])), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); + run_ast(file, "substract_days(a, b)", &[ + ("a", DataType::Date, from_date_data(vec![-100, 0, 100])), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); +} + +fn test_timestamp_arithmetic_functions(file: &mut impl Write) { + run_ast(file, "add_years(to_timestamp(0), 10000)", &[]); // failed + run_ast(file, "add_years(to_timestamp(0), 100)", &[]); + run_ast(file, "add_months(to_timestamp(0), 100)", &[]); + run_ast(file, "add_days(to_timestamp(0), 100)", &[]); + run_ast(file, "add_hours(to_timestamp(0), 100)", &[]); + run_ast(file, "add_minutes(to_timestamp(0), 100)", &[]); + run_ast(file, "add_seconds(to_timestamp(0), 100)", &[]); + run_ast(file, "substract_years(to_timestamp(0), 100)", &[]); + run_ast(file, "substract_months(to_timestamp(0), 100)", &[]); + run_ast(file, "substract_days(to_timestamp(0), 100)", &[]); + run_ast(file, "substract_hours(to_timestamp(0), 100)", &[]); + run_ast(file, "substract_minutes(to_timestamp(0), 100)", &[]); + run_ast(file, "substract_seconds(to_timestamp(0), 100)", &[]); + run_ast(file, "add_years(a, b)", &[ + ( + "a", + DataType::Timestamp, + from_timestamp_data(vec![-100, 0, 100]), + ), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); + run_ast(file, "add_months(a, b)", &[ + ( + "a", + DataType::Timestamp, + from_timestamp_data(vec![-100, 0, 100]), + ), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); + run_ast(file, "add_days(a, b)", &[ + ( + "a", + DataType::Timestamp, + from_timestamp_data(vec![-100, 0, 100]), + ), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); + run_ast(file, "add_hours(a, b)", &[ + ( + "a", + DataType::Timestamp, + from_timestamp_data(vec![-100, 0, 100]), + ), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); + run_ast(file, "add_minutes(a, b)", &[ + ( + "a", + DataType::Timestamp, + from_timestamp_data(vec![-100, 0, 100]), + ), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); + run_ast(file, "add_seconds(a, b)", &[ + ( + "a", + DataType::Timestamp, + from_timestamp_data(vec![-100, 0, 100]), + ), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); + run_ast(file, "substract_years(a, b)", &[ + ( + "a", + DataType::Timestamp, + from_timestamp_data(vec![-100, 0, 100]), + ), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); + run_ast(file, "substract_months(a, b)", &[ + ( + "a", + DataType::Timestamp, + from_timestamp_data(vec![-100, 0, 100]), + ), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); + run_ast(file, "substract_days(a, b)", &[ + ( + "a", + DataType::Timestamp, + from_timestamp_data(vec![-100, 0, 100]), + ), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); + run_ast(file, "substract_hours(a, b)", &[ + ( + "a", + DataType::Timestamp, + from_timestamp_data(vec![-100, 0, 100]), + ), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); + run_ast(file, "substract_minutes(a, b)", &[ + ( + "a", + DataType::Timestamp, + from_timestamp_data(vec![-100, 0, 100]), + ), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); + run_ast(file, "substract_seconds(a, b)", &[ + ( + "a", + DataType::Timestamp, + from_timestamp_data(vec![-100, 0, 100]), + ), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); +} diff --git a/src/query/functions-v2/tests/it/scalars/testdata/datetime.txt b/src/query/functions-v2/tests/it/scalars/testdata/datetime.txt index 31e2582697a4..dfc5e809b42b 100644 --- a/src/query/functions-v2/tests/it/scalars/testdata/datetime.txt +++ b/src/query/functions-v2/tests/it/scalars/testdata/datetime.txt @@ -320,3 +320,595 @@ evaluation (internal): +--------+-----------------------------------------+ +error: + --> SQL:1:1 + | +1 | add_years(to_date(0), 10000) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ date `+11970-01-01` is out of range + + + +ast : add_years(to_date(0), 100) +raw expr : add_years(to_date(0_u8), 100_u8) +checked expr : add_years(to_date(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : 36525 +output type : Date +output domain : Unknown +output : 2070-01-01 + + +ast : add_months(to_date(0), 100) +raw expr : add_months(to_date(0_u8), 100_u8) +checked expr : add_months(to_date(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : 3042 +output type : Date +output domain : Unknown +output : 1978-05-01 + + +ast : add_days(to_date(0), 100) +raw expr : add_days(to_date(0_u8), 100_u8) +checked expr : add_days(to_date(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : 100 +output type : Date +output domain : Unknown +output : 1970-04-11 + + +ast : substract_years(to_date(0), 100) +raw expr : substract_years(to_date(0_u8), 100_u8) +checked expr : substract_years(to_date(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : -36524 +output type : Date +output domain : Unknown +output : 1870-01-01 + + +ast : substract_months(to_date(0), 100) +raw expr : substract_months(to_date(0_u8), 100_u8) +checked expr : substract_months(to_date(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : -3044 +output type : Date +output domain : Unknown +output : 1961-09-01 + + +ast : substract_days(to_date(0), 100) +raw expr : substract_days(to_date(0_u8), 100_u8) +checked expr : substract_days(to_date(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : -100 +output type : Date +output domain : Unknown +output : 1969-09-23 + + +ast : add_years(a, b) +raw expr : add_years(ColumnRef(0)::Date, ColumnRef(1)::Int32) +checked expr : add_years(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+--------------+---------+------------+ +| | a | b | Output | ++--------+--------------+---------+------------+ +| Type | Date | Int32 | Date | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-09-23 | 1 | 1970-09-23 | +| Row 1 | 1970-01-01 | 2 | 1972-01-01 | +| Row 2 | 1970-04-11 | 3 | 1973-04-11 | ++--------+--------------+---------+------------+ +evaluation (internal): ++--------+------------------+ +| Column | Data | ++--------+------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [265, 730, 1196] | ++--------+------------------+ + + +ast : add_months(a, b) +raw expr : add_months(ColumnRef(0)::Date, ColumnRef(1)::Int32) +checked expr : add_months(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+--------------+---------+------------+ +| | a | b | Output | ++--------+--------------+---------+------------+ +| Type | Date | Int32 | Date | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-09-23 | 1 | 1969-10-23 | +| Row 1 | 1970-01-01 | 2 | 1970-03-01 | +| Row 2 | 1970-04-11 | 3 | 1970-07-11 | ++--------+--------------+---------+------------+ +evaluation (internal): ++--------+------------------+ +| Column | Data | ++--------+------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [-70, 59, 191] | ++--------+------------------+ + + +ast : add_days(a, b) +raw expr : add_days(ColumnRef(0)::Date, ColumnRef(1)::Int32) +checked expr : add_days(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+--------------+---------+------------+ +| | a | b | Output | ++--------+--------------+---------+------------+ +| Type | Date | Int32 | Date | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-09-23 | 1 | 1969-09-24 | +| Row 1 | 1970-01-01 | 2 | 1970-01-03 | +| Row 2 | 1970-04-11 | 3 | 1970-04-14 | ++--------+--------------+---------+------------+ +evaluation (internal): ++--------+------------------+ +| Column | Data | ++--------+------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [-99, 2, 103] | ++--------+------------------+ + + +ast : substract_years(a, b) +raw expr : substract_years(ColumnRef(0)::Date, ColumnRef(1)::Int32) +checked expr : substract_years(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+--------------+---------+------------+ +| | a | b | Output | ++--------+--------------+---------+------------+ +| Type | Date | Int32 | Date | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-09-23 | 1 | 1968-09-23 | +| Row 1 | 1970-01-01 | 2 | 1968-01-01 | +| Row 2 | 1970-04-11 | 3 | 1967-04-11 | ++--------+--------------+---------+------------+ +evaluation (internal): ++--------+--------------------+ +| Column | Data | ++--------+--------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [-465, -731, -996] | ++--------+--------------------+ + + +ast : substract_months(a, b) +raw expr : substract_months(ColumnRef(0)::Date, ColumnRef(1)::Int32) +checked expr : substract_months(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+--------------+---------+------------+ +| | a | b | Output | ++--------+--------------+---------+------------+ +| Type | Date | Int32 | Date | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-09-23 | 1 | 1969-08-23 | +| Row 1 | 1970-01-01 | 2 | 1969-11-01 | +| Row 2 | 1970-04-11 | 3 | 1970-01-11 | ++--------+--------------+---------+------------+ +evaluation (internal): ++--------+------------------+ +| Column | Data | ++--------+------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [-131, -61, 10] | ++--------+------------------+ + + +ast : substract_days(a, b) +raw expr : substract_days(ColumnRef(0)::Date, ColumnRef(1)::Int32) +checked expr : substract_days(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+--------------+---------+------------+ +| | a | b | Output | ++--------+--------------+---------+------------+ +| Type | Date | Int32 | Date | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-09-23 | 1 | 1969-09-22 | +| Row 1 | 1970-01-01 | 2 | 1969-12-30 | +| Row 2 | 1970-04-11 | 3 | 1970-04-08 | ++--------+--------------+---------+------------+ +evaluation (internal): ++--------+------------------+ +| Column | Data | ++--------+------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [-101, -2, 97] | ++--------+------------------+ + + +error: + --> SQL:1:1 + | +1 | add_years(to_timestamp(0), 10000) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ timestamp `315569520000000000` is out of range + + + +ast : add_years(to_timestamp(0), 100) +raw expr : add_years(to_timestamp(0_u8), 100_u8) +checked expr : add_years(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : 3155760000000000 +output type : Timestamp +output domain : Unknown +output : 2070-01-01 00:00:00.000000 + + +ast : add_months(to_timestamp(0), 100) +raw expr : add_months(to_timestamp(0_u8), 100_u8) +checked expr : add_months(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : 262828800000000 +output type : Timestamp +output domain : Unknown +output : 1978-05-01 00:00:00.000000 + + +ast : add_days(to_timestamp(0), 100) +raw expr : add_days(to_timestamp(0_u8), 100_u8) +checked expr : add_days(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : 8640000000000 +output type : Timestamp +output domain : Unknown +output : 1970-04-11 00:00:00.000000 + + +ast : add_hours(to_timestamp(0), 100) +raw expr : add_hours(to_timestamp(0_u8), 100_u8) +checked expr : add_hours(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : 360000000000 +output type : Timestamp +output domain : Unknown +output : 1970-01-05 04:00:00.000000 + + +ast : add_minutes(to_timestamp(0), 100) +raw expr : add_minutes(to_timestamp(0_u8), 100_u8) +checked expr : add_minutes(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : 6000000000 +output type : Timestamp +output domain : Unknown +output : 1970-01-01 01:40:00.000000 + + +ast : add_seconds(to_timestamp(0), 100) +raw expr : add_seconds(to_timestamp(0_u8), 100_u8) +checked expr : add_seconds(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : 100000000 +output type : Timestamp +output domain : Unknown +output : 1970-01-01 00:01:40.000000 + + +ast : substract_years(to_timestamp(0), 100) +raw expr : substract_years(to_timestamp(0_u8), 100_u8) +checked expr : substract_years(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : -3155673600000000 +output type : Timestamp +output domain : Unknown +output : 1870-01-01 00:00:00.000000 + + +ast : substract_months(to_timestamp(0), 100) +raw expr : substract_months(to_timestamp(0_u8), 100_u8) +checked expr : substract_months(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : -263001600000000 +output type : Timestamp +output domain : Unknown +output : 1961-09-01 00:00:00.000000 + + +ast : substract_days(to_timestamp(0), 100) +raw expr : substract_days(to_timestamp(0_u8), 100_u8) +checked expr : substract_days(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : -8640000000000 +output type : Timestamp +output domain : Unknown +output : 1969-09-23 00:00:00.000000 + + +ast : substract_hours(to_timestamp(0), 100) +raw expr : substract_hours(to_timestamp(0_u8), 100_u8) +checked expr : substract_hours(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : -360000000000 +output type : Timestamp +output domain : Unknown +output : 1969-12-27 20:00:00.000000 + + +ast : substract_minutes(to_timestamp(0), 100) +raw expr : substract_minutes(to_timestamp(0_u8), 100_u8) +checked expr : substract_minutes(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : -6000000000 +output type : Timestamp +output domain : Unknown +output : 1969-12-31 22:20:00.000000 + + +ast : substract_seconds(to_timestamp(0), 100) +raw expr : substract_seconds(to_timestamp(0_u8), 100_u8) +checked expr : substract_seconds(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : -100000000 +output type : Timestamp +output domain : Unknown +output : 1969-12-31 23:58:20.000000 + + +ast : add_years(a, b) +raw expr : add_years(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) +checked expr : add_years(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+----------------------------+---------+----------------------------+ +| | a | b | Output | ++--------+----------------------------+---------+----------------------------+ +| Type | Timestamp | Int32 | Timestamp | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-12-31 23:59:59.999900 | 1 | 1970-12-31 23:59:59.999900 | +| Row 1 | 1970-01-01 00:00:00.000000 | 2 | 1972-01-01 00:00:00.000000 | +| Row 2 | 1970-01-01 00:00:00.000100 | 3 | 1973-01-01 00:00:00.000100 | ++--------+----------------------------+---------+----------------------------+ +evaluation (internal): ++--------+--------------------------------------------------+ +| Column | Data | ++--------+--------------------------------------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [31535999999900, 63072000000000, 94694400000100] | ++--------+--------------------------------------------------+ + + +ast : add_months(a, b) +raw expr : add_months(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) +checked expr : add_months(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+----------------------------+---------+----------------------------+ +| | a | b | Output | ++--------+----------------------------+---------+----------------------------+ +| Type | Timestamp | Int32 | Timestamp | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-12-31 23:59:59.999900 | 1 | 1970-01-31 23:59:59.999900 | +| Row 1 | 1970-01-01 00:00:00.000000 | 2 | 1970-03-01 00:00:00.000000 | +| Row 2 | 1970-01-01 00:00:00.000100 | 3 | 1970-04-01 00:00:00.000100 | ++--------+----------------------------+---------+----------------------------+ +evaluation (internal): ++--------+-----------------------------------------------+ +| Column | Data | ++--------+-----------------------------------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [2678399999900, 5097600000000, 7776000000100] | ++--------+-----------------------------------------------+ + + +ast : add_days(a, b) +raw expr : add_days(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) +checked expr : add_days(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+----------------------------+---------+----------------------------+ +| | a | b | Output | ++--------+----------------------------+---------+----------------------------+ +| Type | Timestamp | Int32 | Timestamp | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-12-31 23:59:59.999900 | 1 | 1970-01-01 23:59:59.999900 | +| Row 1 | 1970-01-01 00:00:00.000000 | 2 | 1970-01-03 00:00:00.000000 | +| Row 2 | 1970-01-01 00:00:00.000100 | 3 | 1970-01-04 00:00:00.000100 | ++--------+----------------------------+---------+----------------------------+ +evaluation (internal): ++--------+-------------------------------------------+ +| Column | Data | ++--------+-------------------------------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [86399999900, 172800000000, 259200000100] | ++--------+-------------------------------------------+ + + +ast : add_hours(a, b) +raw expr : add_hours(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) +checked expr : add_hours(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+----------------------------+---------+----------------------------+ +| | a | b | Output | ++--------+----------------------------+---------+----------------------------+ +| Type | Timestamp | Int32 | Timestamp | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-12-31 23:59:59.999900 | 1 | 1970-01-01 00:59:59.999900 | +| Row 1 | 1970-01-01 00:00:00.000000 | 2 | 1970-01-01 02:00:00.000000 | +| Row 2 | 1970-01-01 00:00:00.000100 | 3 | 1970-01-01 03:00:00.000100 | ++--------+----------------------------+---------+----------------------------+ +evaluation (internal): ++--------+---------------------------------------+ +| Column | Data | ++--------+---------------------------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [3599999900, 7200000000, 10800000100] | ++--------+---------------------------------------+ + + +ast : add_minutes(a, b) +raw expr : add_minutes(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) +checked expr : add_minutes(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+----------------------------+---------+----------------------------+ +| | a | b | Output | ++--------+----------------------------+---------+----------------------------+ +| Type | Timestamp | Int32 | Timestamp | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-12-31 23:59:59.999900 | 1 | 1970-01-01 00:00:59.999900 | +| Row 1 | 1970-01-01 00:00:00.000000 | 2 | 1970-01-01 00:02:00.000000 | +| Row 2 | 1970-01-01 00:00:00.000100 | 3 | 1970-01-01 00:03:00.000100 | ++--------+----------------------------+---------+----------------------------+ +evaluation (internal): ++--------+----------------------------------+ +| Column | Data | ++--------+----------------------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [59999900, 120000000, 180000100] | ++--------+----------------------------------+ + + +ast : add_seconds(a, b) +raw expr : add_seconds(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) +checked expr : add_seconds(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+----------------------------+---------+----------------------------+ +| | a | b | Output | ++--------+----------------------------+---------+----------------------------+ +| Type | Timestamp | Int32 | Timestamp | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-12-31 23:59:59.999900 | 1 | 1970-01-01 00:00:00.999900 | +| Row 1 | 1970-01-01 00:00:00.000000 | 2 | 1970-01-01 00:00:02.000000 | +| Row 2 | 1970-01-01 00:00:00.000100 | 3 | 1970-01-01 00:00:03.000100 | ++--------+----------------------------+---------+----------------------------+ +evaluation (internal): ++--------+----------------------------+ +| Column | Data | ++--------+----------------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [999900, 2000000, 3000100] | ++--------+----------------------------+ + + +ast : substract_years(a, b) +raw expr : substract_years(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) +checked expr : substract_years(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+----------------------------+---------+----------------------------+ +| | a | b | Output | ++--------+----------------------------+---------+----------------------------+ +| Type | Timestamp | Int32 | Timestamp | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-12-31 23:59:59.999900 | 1 | 1968-12-31 23:59:59.999900 | +| Row 1 | 1970-01-01 00:00:00.000000 | 2 | 1968-01-01 00:00:00.000000 | +| Row 2 | 1970-01-01 00:00:00.000100 | 3 | 1967-01-01 00:00:00.000100 | ++--------+----------------------------+---------+----------------------------+ +evaluation (internal): ++--------+-----------------------------------------------------+ +| Column | Data | ++--------+-----------------------------------------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [-31536000000100, -63158400000000, -94694399999900] | ++--------+-----------------------------------------------------+ + + +ast : substract_months(a, b) +raw expr : substract_months(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) +checked expr : substract_months(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+----------------------------+---------+----------------------------+ +| | a | b | Output | ++--------+----------------------------+---------+----------------------------+ +| Type | Timestamp | Int32 | Timestamp | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-12-31 23:59:59.999900 | 1 | 1969-11-30 23:59:59.999900 | +| Row 1 | 1970-01-01 00:00:00.000000 | 2 | 1969-11-01 00:00:00.000000 | +| Row 2 | 1970-01-01 00:00:00.000100 | 3 | 1969-10-01 00:00:00.000100 | ++--------+----------------------------+---------+----------------------------+ +evaluation (internal): ++--------+--------------------------------------------------+ +| Column | Data | ++--------+--------------------------------------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [-2678400000100, -5270400000000, -7948799999900] | ++--------+--------------------------------------------------+ + + +ast : substract_days(a, b) +raw expr : substract_days(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) +checked expr : substract_days(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+----------------------------+---------+----------------------------+ +| | a | b | Output | ++--------+----------------------------+---------+----------------------------+ +| Type | Timestamp | Int32 | Timestamp | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-12-31 23:59:59.999900 | 1 | 1969-12-30 23:59:59.999900 | +| Row 1 | 1970-01-01 00:00:00.000000 | 2 | 1969-12-30 00:00:00.000000 | +| Row 2 | 1970-01-01 00:00:00.000100 | 3 | 1969-12-29 00:00:00.000100 | ++--------+----------------------------+---------+----------------------------+ +evaluation (internal): ++--------+----------------------------------------------+ +| Column | Data | ++--------+----------------------------------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [-86400000100, -172800000000, -259199999900] | ++--------+----------------------------------------------+ + + +ast : substract_hours(a, b) +raw expr : substract_hours(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) +checked expr : substract_hours(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+----------------------------+---------+----------------------------+ +| | a | b | Output | ++--------+----------------------------+---------+----------------------------+ +| Type | Timestamp | Int32 | Timestamp | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-12-31 23:59:59.999900 | 1 | 1969-12-31 22:59:59.999900 | +| Row 1 | 1970-01-01 00:00:00.000000 | 2 | 1969-12-31 22:00:00.000000 | +| Row 2 | 1970-01-01 00:00:00.000100 | 3 | 1969-12-31 21:00:00.000100 | ++--------+----------------------------+---------+----------------------------+ +evaluation (internal): ++--------+------------------------------------------+ +| Column | Data | ++--------+------------------------------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [-3600000100, -7200000000, -10799999900] | ++--------+------------------------------------------+ + + +ast : substract_minutes(a, b) +raw expr : substract_minutes(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) +checked expr : substract_minutes(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+----------------------------+---------+----------------------------+ +| | a | b | Output | ++--------+----------------------------+---------+----------------------------+ +| Type | Timestamp | Int32 | Timestamp | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-12-31 23:59:59.999900 | 1 | 1969-12-31 23:58:59.999900 | +| Row 1 | 1970-01-01 00:00:00.000000 | 2 | 1969-12-31 23:58:00.000000 | +| Row 2 | 1970-01-01 00:00:00.000100 | 3 | 1969-12-31 23:57:00.000100 | ++--------+----------------------------+---------+----------------------------+ +evaluation (internal): ++--------+-------------------------------------+ +| Column | Data | ++--------+-------------------------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [-60000100, -120000000, -179999900] | ++--------+-------------------------------------+ + + +ast : substract_seconds(a, b) +raw expr : substract_seconds(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) +checked expr : substract_seconds(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+----------------------------+---------+----------------------------+ +| | a | b | Output | ++--------+----------------------------+---------+----------------------------+ +| Type | Timestamp | Int32 | Timestamp | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-12-31 23:59:59.999900 | 1 | 1969-12-31 23:59:58.999900 | +| Row 1 | 1970-01-01 00:00:00.000000 | 2 | 1969-12-31 23:59:58.000000 | +| Row 2 | 1970-01-01 00:00:00.000100 | 3 | 1969-12-31 23:59:57.000100 | ++--------+----------------------------+---------+----------------------------+ +evaluation (internal): ++--------+--------------------------------+ +| Column | Data | ++--------+--------------------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [-1000100, -2000000, -2999900] | ++--------+--------------------------------+ + + From 2b00767afea7e48c0a59db0d58a916d782a1f6ab Mon Sep 17 00:00:00 2001 From: RinChanNOWWW Date: Fri, 14 Oct 2022 17:23:13 +0800 Subject: [PATCH 3/8] Add `date_add` and `date_sub`. --- Cargo.lock | 1 + src/query/functions-v2/Cargo.toml | 1 + .../functions-v2/src/scalars/datetime.rs | 21 + .../functions-v2/tests/it/scalars/datetime.rs | 317 ++++++- .../functions-v2/tests/it/scalars/parser.rs | 55 ++ .../tests/it/scalars/testdata/datetime.txt | 839 ++++++++++++++++++ 6 files changed, 1230 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8d16cb05d4b6..2d22144a6c29 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1462,6 +1462,7 @@ dependencies = [ "comfy-table", "common-arrow", "common-ast", + "common-datavalues", "common-exception", "common-expression", "common-hashtable", diff --git a/src/query/functions-v2/Cargo.toml b/src/query/functions-v2/Cargo.toml index c5347967386c..f694203dd7cf 100644 --- a/src/query/functions-v2/Cargo.toml +++ b/src/query/functions-v2/Cargo.toml @@ -12,6 +12,7 @@ doctest = false [dependencies] # In alphabetical order # Workspace dependencies common-arrow = { path = "../../common/arrow" } +common-datavalues = { path = "../datavalues" } common-exception = { path = "../../common/exception" } common-expression = { path = "../expression" } common-hashtable = { path = "../../common/hashtable" } diff --git a/src/query/functions-v2/src/scalars/datetime.rs b/src/query/functions-v2/src/scalars/datetime.rs index f215b432b4f1..1777eeb31581 100644 --- a/src/query/functions-v2/src/scalars/datetime.rs +++ b/src/query/functions-v2/src/scalars/datetime.rs @@ -285,6 +285,27 @@ macro_rules! impl_register_arith_functions { }), ); + registry.register_passthrough_nullable_2_arg::( + concat!($op, "_quarters"), + FunctionProperty::default(), + |_, _| None, + vectorize_with_builder_2_arg::( + |ts, delta, builder, _| { + builder.push(AddMonthsImpl::eval_timestamp(ts, $signed_wrapper!{delta} * 3)?); + Ok(()) + }, + ), + ); + registry.register_passthrough_nullable_2_arg::( + concat!($op, "_quarters"), + FunctionProperty::default(), + |_, _| None, + vectorize_with_builder_2_arg::(|date, delta, builder, _| { + builder.push(AddMonthsImpl::eval_date(date, $signed_wrapper!{delta} * 3)?); + Ok(()) + }), + ); + registry.register_passthrough_nullable_2_arg::( concat!($op, "_months"), FunctionProperty::default(), diff --git a/src/query/functions-v2/tests/it/scalars/datetime.rs b/src/query/functions-v2/tests/it/scalars/datetime.rs index ebb04cac2fcd..0ce73b99f4b1 100644 --- a/src/query/functions-v2/tests/it/scalars/datetime.rs +++ b/src/query/functions-v2/tests/it/scalars/datetime.rs @@ -32,8 +32,12 @@ fn test_datetime() { test_to_timestamp(file); test_to_datetime(file); test_to_date(file); - test_date_arithmetic_functions(file); - test_timestamp_arithmetic_functions(file); + // {add | subtract}_{years | months | days | hours | minutes | seconds}(date, number) + test_date_add_substract(file); + test_timestamp_add_substract(file); + // date_{add | sub}({year | quarter | month | week | day | hour | minute | second}, date, number) + test_date_date_add_sub(file); + test_timestamp_date_add_sub(file); } fn test_to_timestamp(file: &mut impl Write) { @@ -104,12 +108,13 @@ fn test_to_date(file: &mut impl Write) { )]); } -fn test_date_arithmetic_functions(file: &mut impl Write) { +fn test_date_add_substract(file: &mut impl Write) { run_ast(file, "add_years(to_date(0), 10000)", &[]); // failed run_ast(file, "add_years(to_date(0), 100)", &[]); run_ast(file, "add_months(to_date(0), 100)", &[]); run_ast(file, "add_days(to_date(0), 100)", &[]); run_ast(file, "substract_years(to_date(0), 100)", &[]); + run_ast(file, "substract_quarters(to_date(0), 100)", &[]); run_ast(file, "substract_months(to_date(0), 100)", &[]); run_ast(file, "substract_days(to_date(0), 100)", &[]); run_ast(file, "add_years(a, b)", &[ @@ -120,6 +125,14 @@ fn test_date_arithmetic_functions(file: &mut impl Write) { Column::from_data(vec![1, 2, 3]), ), ]); + run_ast(file, "add_quarters(a, b)", &[ + ("a", DataType::Date, from_date_data(vec![-100, 0, 100])), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); run_ast(file, "add_months(a, b)", &[ ("a", DataType::Date, from_date_data(vec![-100, 0, 100])), ( @@ -144,6 +157,14 @@ fn test_date_arithmetic_functions(file: &mut impl Write) { Column::from_data(vec![1, 2, 3]), ), ]); + run_ast(file, "substract_quarters(a, b)", &[ + ("a", DataType::Date, from_date_data(vec![-100, 0, 100])), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); run_ast(file, "substract_months(a, b)", &[ ("a", DataType::Date, from_date_data(vec![-100, 0, 100])), ( @@ -162,15 +183,17 @@ fn test_date_arithmetic_functions(file: &mut impl Write) { ]); } -fn test_timestamp_arithmetic_functions(file: &mut impl Write) { +fn test_timestamp_add_substract(file: &mut impl Write) { run_ast(file, "add_years(to_timestamp(0), 10000)", &[]); // failed run_ast(file, "add_years(to_timestamp(0), 100)", &[]); + run_ast(file, "add_quarters(to_timestamp(0), 100)", &[]); run_ast(file, "add_months(to_timestamp(0), 100)", &[]); run_ast(file, "add_days(to_timestamp(0), 100)", &[]); run_ast(file, "add_hours(to_timestamp(0), 100)", &[]); run_ast(file, "add_minutes(to_timestamp(0), 100)", &[]); run_ast(file, "add_seconds(to_timestamp(0), 100)", &[]); run_ast(file, "substract_years(to_timestamp(0), 100)", &[]); + run_ast(file, "substract_quarters(to_timestamp(0), 100)", &[]); run_ast(file, "substract_months(to_timestamp(0), 100)", &[]); run_ast(file, "substract_days(to_timestamp(0), 100)", &[]); run_ast(file, "substract_hours(to_timestamp(0), 100)", &[]); @@ -188,6 +211,18 @@ fn test_timestamp_arithmetic_functions(file: &mut impl Write) { Column::from_data(vec![1, 2, 3]), ), ]); + run_ast(file, "add_quarters(a, b)", &[ + ( + "a", + DataType::Timestamp, + from_timestamp_data(vec![-100, 0, 100]), + ), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); run_ast(file, "add_months(a, b)", &[ ( "a", @@ -260,6 +295,18 @@ fn test_timestamp_arithmetic_functions(file: &mut impl Write) { Column::from_data(vec![1, 2, 3]), ), ]); + run_ast(file, "substract_quarters(a, b)", &[ + ( + "a", + DataType::Timestamp, + from_timestamp_data(vec![-100, 0, 100]), + ), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); run_ast(file, "substract_months(a, b)", &[ ( "a", @@ -321,3 +368,265 @@ fn test_timestamp_arithmetic_functions(file: &mut impl Write) { ), ]); } + +fn test_date_date_add_sub(file: &mut impl Write) { + run_ast(file, "date_add(year, 10000, to_date(0))", &[]); // failed + run_ast(file, "date_add(year, 100, to_date(0))", &[]); + run_ast(file, "date_add(quarter, 100, to_date(0))", &[]); + run_ast(file, "date_add(month, 100, to_date(0))", &[]); + run_ast(file, "date_add(day, 100, to_date(0))", &[]); + run_ast(file, "date_sub(year, 100, to_date(0))", &[]); + run_ast(file, "date_sub(quarter, 100, to_date(0))", &[]); + run_ast(file, "date_sub(month, 100, to_date(0))", &[]); + run_ast(file, "date_sub(day, 100, to_date(0))", &[]); + run_ast(file, "date_add(year, b, a)", &[ + ("a", DataType::Date, from_date_data(vec![-100, 0, 100])), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); + run_ast(file, "date_add(quarter, b, a)", &[ + ("a", DataType::Date, from_date_data(vec![-100, 0, 100])), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); + run_ast(file, "date_add(month, b, a)", &[ + ("a", DataType::Date, from_date_data(vec![-100, 0, 100])), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); + run_ast(file, "date_add(day, b, a)", &[ + ("a", DataType::Date, from_date_data(vec![-100, 0, 100])), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); + run_ast(file, "date_sub(year, b, a)", &[ + ("a", DataType::Date, from_date_data(vec![-100, 0, 100])), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); + run_ast(file, "date_sub(quarter, b, a)", &[ + ("a", DataType::Date, from_date_data(vec![-100, 0, 100])), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); + run_ast(file, "date_sub(month, b, a)", &[ + ("a", DataType::Date, from_date_data(vec![-100, 0, 100])), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); + run_ast(file, "date_sub(day, b, a)", &[ + ("a", DataType::Date, from_date_data(vec![-100, 0, 100])), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); +} + +fn test_timestamp_date_add_sub(file: &mut impl Write) { + run_ast(file, "date_add(year, 10000, to_timestamp(0))", &[]); // failed + run_ast(file, "date_add(year, 100, to_timestamp(0))", &[]); + run_ast(file, "date_add(quarter, 100, to_timestamp(0))", &[]); + run_ast(file, "date_add(month, 100, to_timestamp(0))", &[]); + run_ast(file, "date_add(day, 100, to_timestamp(0))", &[]); + run_ast(file, "date_add(hour, 100, to_timestamp(0))", &[]); + run_ast(file, "date_add(minute, 100, to_timestamp(0))", &[]); + run_ast(file, "date_add(second, 100, to_timestamp(0))", &[]); + run_ast(file, "date_sub(year, 100, to_timestamp(0))", &[]); + run_ast(file, "date_sub(quarter, 100, to_timestamp(0))", &[]); + run_ast(file, "date_sub(month, 100, to_timestamp(0))", &[]); + run_ast(file, "date_sub(day, 100, to_timestamp(0))", &[]); + run_ast(file, "date_sub(hour, 100, to_timestamp(0))", &[]); + run_ast(file, "date_sub(minute, 100, to_timestamp(0))", &[]); + run_ast(file, "date_sub(second, 100, to_timestamp(0))", &[]); + run_ast(file, "date_add(year, b, a)", &[ + ( + "a", + DataType::Timestamp, + from_timestamp_data(vec![-100, 0, 100]), + ), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); + run_ast(file, "date_add(quarter, b, a)", &[ + ( + "a", + DataType::Timestamp, + from_timestamp_data(vec![-100, 0, 100]), + ), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); + run_ast(file, "date_add(month, b, a)", &[ + ( + "a", + DataType::Timestamp, + from_timestamp_data(vec![-100, 0, 100]), + ), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); + run_ast(file, "date_add(day, b, a)", &[ + ( + "a", + DataType::Timestamp, + from_timestamp_data(vec![-100, 0, 100]), + ), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); + run_ast(file, "date_add(hour, b, a)", &[ + ( + "a", + DataType::Timestamp, + from_timestamp_data(vec![-100, 0, 100]), + ), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); + run_ast(file, "date_add(minute, b, a)", &[ + ( + "a", + DataType::Timestamp, + from_timestamp_data(vec![-100, 0, 100]), + ), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); + run_ast(file, "date_add(second, b, a)", &[ + ( + "a", + DataType::Timestamp, + from_timestamp_data(vec![-100, 0, 100]), + ), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); + run_ast(file, "date_sub(year, b, a)", &[ + ( + "a", + DataType::Timestamp, + from_timestamp_data(vec![-100, 0, 100]), + ), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); + run_ast(file, "date_sub(quarter, b, a)", &[ + ( + "a", + DataType::Timestamp, + from_timestamp_data(vec![-100, 0, 100]), + ), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); + run_ast(file, "date_sub(month, b, a)", &[ + ( + "a", + DataType::Timestamp, + from_timestamp_data(vec![-100, 0, 100]), + ), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); + run_ast(file, "date_sub(day, b, a)", &[ + ( + "a", + DataType::Timestamp, + from_timestamp_data(vec![-100, 0, 100]), + ), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); + run_ast(file, "date_sub(hour, b, a)", &[ + ( + "a", + DataType::Timestamp, + from_timestamp_data(vec![-100, 0, 100]), + ), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); + run_ast(file, "date_sub(minute, b, a)", &[ + ( + "a", + DataType::Timestamp, + from_timestamp_data(vec![-100, 0, 100]), + ), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); + run_ast(file, "date_sub(second, b, a)", &[ + ( + "a", + DataType::Timestamp, + from_timestamp_data(vec![-100, 0, 100]), + ), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); +} diff --git a/src/query/functions-v2/tests/it/scalars/parser.rs b/src/query/functions-v2/tests/it/scalars/parser.rs index 269f59ee3cf7..87569eef13b9 100644 --- a/src/query/functions-v2/tests/it/scalars/parser.rs +++ b/src/query/functions-v2/tests/it/scalars/parser.rs @@ -21,6 +21,7 @@ use common_ast::parser::token::Token; use common_ast::parser::tokenize_sql; use common_ast::Backtrace; use common_ast::Dialect; +use common_datavalues::IntervalKind; use common_expression::types::DataType; use common_expression::types::NumberDataType; use common_expression::Literal; @@ -34,6 +35,18 @@ pub fn parse_raw_expr(text: &str, columns: &[(&str, DataType)]) -> RawExpr { transform_expr(expr, columns) } +macro_rules! with_interval_mapped_name { + (| $t:tt | $($tail:tt)*) => { + match_template::match_template! { + $t = [ + Year => "years", Quarter => "quarters", Month => "months", Day => "days", + Hour => "hours", Minute => "minutes", Second => "seconds", + ], + $($tail)* + } + } +} + pub fn transform_expr(ast: common_ast::ast::Expr, columns: &[(&str, DataType)]) -> RawExpr { match ast { common_ast::ast::Expr::Literal { span, lit } => RawExpr::Literal { @@ -288,6 +301,48 @@ pub fn transform_expr(ast: common_ast::ast::Expr, columns: &[(&str, DataType)]) } } } + common_ast::ast::Expr::DateAdd { + span, + unit, + interval, + date, + } => { + with_interval_mapped_name!(|INTERVAL| match unit { + IntervalKind::INTERVAL => RawExpr::FunctionCall { + span: transform_span(span), + name: concat!("add_", INTERVAL).to_string(), + params: vec![], + args: vec![ + transform_expr(*date, columns), + transform_expr(*interval, columns), + ], + }, + kind => { + unimplemented!("{kind:?} is not supported") + } + }) + } + common_ast::ast::Expr::DateSub { + span, + unit, + interval, + date, + } => { + with_interval_mapped_name!(|INTERVAL| match unit { + IntervalKind::INTERVAL => RawExpr::FunctionCall { + span: transform_span(span), + name: concat!("substract_", INTERVAL).to_string(), + params: vec![], + args: vec![ + transform_expr(*date, columns), + transform_expr(*interval, columns), + ], + }, + kind => { + unimplemented!("{kind:?} is not supported") + } + }) + } expr => unimplemented!("{expr:?} is unimplemented"), } } diff --git a/src/query/functions-v2/tests/it/scalars/testdata/datetime.txt b/src/query/functions-v2/tests/it/scalars/testdata/datetime.txt index dfc5e809b42b..9db5a841cf4c 100644 --- a/src/query/functions-v2/tests/it/scalars/testdata/datetime.txt +++ b/src/query/functions-v2/tests/it/scalars/testdata/datetime.txt @@ -364,6 +364,15 @@ output domain : Unknown output : 1870-01-01 +ast : substract_quarters(to_date(0), 100) +raw expr : substract_quarters(to_date(0_u8), 100_u8) +checked expr : substract_quarters(to_date(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : -9131 +output type : Date +output domain : Unknown +output : 1945-01-01 + + ast : substract_months(to_date(0), 100) raw expr : substract_months(to_date(0_u8), 100_u8) checked expr : substract_months(to_date(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) @@ -405,6 +414,29 @@ evaluation (internal): +--------+------------------+ +ast : add_quarters(a, b) +raw expr : add_quarters(ColumnRef(0)::Date, ColumnRef(1)::Int32) +checked expr : add_quarters(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+--------------+---------+------------+ +| | a | b | Output | ++--------+--------------+---------+------------+ +| Type | Date | Int32 | Date | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-09-23 | 1 | 1969-12-23 | +| Row 1 | 1970-01-01 | 2 | 1970-07-01 | +| Row 2 | 1970-04-11 | 3 | 1971-01-11 | ++--------+--------------+---------+------------+ +evaluation (internal): ++--------+------------------+ +| Column | Data | ++--------+------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [-9, 181, 375] | ++--------+------------------+ + + ast : add_months(a, b) raw expr : add_months(ColumnRef(0)::Date, ColumnRef(1)::Int32) checked expr : add_months(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) @@ -474,6 +506,29 @@ evaluation (internal): +--------+--------------------+ +ast : substract_quarters(a, b) +raw expr : substract_quarters(ColumnRef(0)::Date, ColumnRef(1)::Int32) +checked expr : substract_quarters(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+--------------+---------+------------+ +| | a | b | Output | ++--------+--------------+---------+------------+ +| Type | Date | Int32 | Date | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-09-23 | 1 | 1969-06-23 | +| Row 1 | 1970-01-01 | 2 | 1969-07-01 | +| Row 2 | 1970-04-11 | 3 | 1969-07-11 | ++--------+--------------+---------+------------+ +evaluation (internal): ++--------+--------------------+ +| Column | Data | ++--------+--------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [-192, -184, -174] | ++--------+--------------------+ + + ast : substract_months(a, b) raw expr : substract_months(ColumnRef(0)::Date, ColumnRef(1)::Int32) checked expr : substract_months(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) @@ -537,6 +592,15 @@ output domain : Unknown output : 2070-01-01 00:00:00.000000 +ast : add_quarters(to_timestamp(0), 100) +raw expr : add_quarters(to_timestamp(0_u8), 100_u8) +checked expr : add_quarters(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : 788918400000000 +output type : Timestamp +output domain : Unknown +output : 1995-01-01 00:00:00.000000 + + ast : add_months(to_timestamp(0), 100) raw expr : add_months(to_timestamp(0_u8), 100_u8) checked expr : add_months(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) @@ -591,6 +655,15 @@ output domain : Unknown output : 1870-01-01 00:00:00.000000 +ast : substract_quarters(to_timestamp(0), 100) +raw expr : substract_quarters(to_timestamp(0_u8), 100_u8) +checked expr : substract_quarters(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : -788918400000000 +output type : Timestamp +output domain : Unknown +output : 1945-01-01 00:00:00.000000 + + ast : substract_months(to_timestamp(0), 100) raw expr : substract_months(to_timestamp(0_u8), 100_u8) checked expr : substract_months(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) @@ -659,6 +732,29 @@ evaluation (internal): +--------+--------------------------------------------------+ +ast : add_quarters(a, b) +raw expr : add_quarters(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) +checked expr : add_quarters(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+----------------------------+---------+----------------------------+ +| | a | b | Output | ++--------+----------------------------+---------+----------------------------+ +| Type | Timestamp | Int32 | Timestamp | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-12-31 23:59:59.999900 | 1 | 1970-03-31 23:59:59.999900 | +| Row 1 | 1970-01-01 00:00:00.000000 | 2 | 1970-07-01 00:00:00.000000 | +| Row 2 | 1970-01-01 00:00:00.000100 | 3 | 1970-10-01 00:00:00.000100 | ++--------+----------------------------+---------+----------------------------+ +evaluation (internal): ++--------+-------------------------------------------------+ +| Column | Data | ++--------+-------------------------------------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [7775999999900, 15638400000000, 23587200000100] | ++--------+-------------------------------------------------+ + + ast : add_months(a, b) raw expr : add_months(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) checked expr : add_months(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) @@ -797,6 +893,29 @@ evaluation (internal): +--------+-----------------------------------------------------+ +ast : substract_quarters(a, b) +raw expr : substract_quarters(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) +checked expr : substract_quarters(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+----------------------------+---------+----------------------------+ +| | a | b | Output | ++--------+----------------------------+---------+----------------------------+ +| Type | Timestamp | Int32 | Timestamp | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-12-31 23:59:59.999900 | 1 | 1969-09-30 23:59:59.999900 | +| Row 1 | 1970-01-01 00:00:00.000000 | 2 | 1969-07-01 00:00:00.000000 | +| Row 2 | 1970-01-01 00:00:00.000100 | 3 | 1969-04-01 00:00:00.000100 | ++--------+----------------------------+---------+----------------------------+ +evaluation (internal): ++--------+----------------------------------------------------+ +| Column | Data | ++--------+----------------------------------------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [-7948800000100, -15897600000000, -23759999999900] | ++--------+----------------------------------------------------+ + + ast : substract_months(a, b) raw expr : substract_months(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) checked expr : substract_months(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) @@ -912,3 +1031,723 @@ evaluation (internal): +--------+--------------------------------+ +error: + --> SQL:1:1 + | +1 | date_add(year, 10000, to_date(0)) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ date `+11970-01-01` is out of range + + + +ast : date_add(year, 100, to_date(0)) +raw expr : add_years(to_date(0_u8), 100_u8) +checked expr : add_years(to_date(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : 36525 +output type : Date +output domain : Unknown +output : 2070-01-01 + + +ast : date_add(quarter, 100, to_date(0)) +raw expr : add_quarters(to_date(0_u8), 100_u8) +checked expr : add_quarters(to_date(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : 9131 +output type : Date +output domain : Unknown +output : 1995-01-01 + + +ast : date_add(month, 100, to_date(0)) +raw expr : add_months(to_date(0_u8), 100_u8) +checked expr : add_months(to_date(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : 3042 +output type : Date +output domain : Unknown +output : 1978-05-01 + + +ast : date_add(day, 100, to_date(0)) +raw expr : add_days(to_date(0_u8), 100_u8) +checked expr : add_days(to_date(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : 100 +output type : Date +output domain : Unknown +output : 1970-04-11 + + +ast : date_sub(year, 100, to_date(0)) +raw expr : substract_years(to_date(0_u8), 100_u8) +checked expr : substract_years(to_date(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : -36524 +output type : Date +output domain : Unknown +output : 1870-01-01 + + +ast : date_sub(quarter, 100, to_date(0)) +raw expr : substract_quarters(to_date(0_u8), 100_u8) +checked expr : substract_quarters(to_date(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : -9131 +output type : Date +output domain : Unknown +output : 1945-01-01 + + +ast : date_sub(month, 100, to_date(0)) +raw expr : substract_months(to_date(0_u8), 100_u8) +checked expr : substract_months(to_date(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : -3044 +output type : Date +output domain : Unknown +output : 1961-09-01 + + +ast : date_sub(day, 100, to_date(0)) +raw expr : substract_days(to_date(0_u8), 100_u8) +checked expr : substract_days(to_date(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : -100 +output type : Date +output domain : Unknown +output : 1969-09-23 + + +ast : date_add(year, b, a) +raw expr : add_years(ColumnRef(0)::Date, ColumnRef(1)::Int32) +checked expr : add_years(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+--------------+---------+------------+ +| | a | b | Output | ++--------+--------------+---------+------------+ +| Type | Date | Int32 | Date | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-09-23 | 1 | 1970-09-23 | +| Row 1 | 1970-01-01 | 2 | 1972-01-01 | +| Row 2 | 1970-04-11 | 3 | 1973-04-11 | ++--------+--------------+---------+------------+ +evaluation (internal): ++--------+------------------+ +| Column | Data | ++--------+------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [265, 730, 1196] | ++--------+------------------+ + + +ast : date_add(quarter, b, a) +raw expr : add_quarters(ColumnRef(0)::Date, ColumnRef(1)::Int32) +checked expr : add_quarters(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+--------------+---------+------------+ +| | a | b | Output | ++--------+--------------+---------+------------+ +| Type | Date | Int32 | Date | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-09-23 | 1 | 1969-12-23 | +| Row 1 | 1970-01-01 | 2 | 1970-07-01 | +| Row 2 | 1970-04-11 | 3 | 1971-01-11 | ++--------+--------------+---------+------------+ +evaluation (internal): ++--------+------------------+ +| Column | Data | ++--------+------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [-9, 181, 375] | ++--------+------------------+ + + +ast : date_add(month, b, a) +raw expr : add_months(ColumnRef(0)::Date, ColumnRef(1)::Int32) +checked expr : add_months(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+--------------+---------+------------+ +| | a | b | Output | ++--------+--------------+---------+------------+ +| Type | Date | Int32 | Date | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-09-23 | 1 | 1969-10-23 | +| Row 1 | 1970-01-01 | 2 | 1970-03-01 | +| Row 2 | 1970-04-11 | 3 | 1970-07-11 | ++--------+--------------+---------+------------+ +evaluation (internal): ++--------+------------------+ +| Column | Data | ++--------+------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [-70, 59, 191] | ++--------+------------------+ + + +ast : date_add(day, b, a) +raw expr : add_days(ColumnRef(0)::Date, ColumnRef(1)::Int32) +checked expr : add_days(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+--------------+---------+------------+ +| | a | b | Output | ++--------+--------------+---------+------------+ +| Type | Date | Int32 | Date | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-09-23 | 1 | 1969-09-24 | +| Row 1 | 1970-01-01 | 2 | 1970-01-03 | +| Row 2 | 1970-04-11 | 3 | 1970-04-14 | ++--------+--------------+---------+------------+ +evaluation (internal): ++--------+------------------+ +| Column | Data | ++--------+------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [-99, 2, 103] | ++--------+------------------+ + + +ast : date_sub(year, b, a) +raw expr : substract_years(ColumnRef(0)::Date, ColumnRef(1)::Int32) +checked expr : substract_years(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+--------------+---------+------------+ +| | a | b | Output | ++--------+--------------+---------+------------+ +| Type | Date | Int32 | Date | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-09-23 | 1 | 1968-09-23 | +| Row 1 | 1970-01-01 | 2 | 1968-01-01 | +| Row 2 | 1970-04-11 | 3 | 1967-04-11 | ++--------+--------------+---------+------------+ +evaluation (internal): ++--------+--------------------+ +| Column | Data | ++--------+--------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [-465, -731, -996] | ++--------+--------------------+ + + +ast : date_sub(quarter, b, a) +raw expr : substract_quarters(ColumnRef(0)::Date, ColumnRef(1)::Int32) +checked expr : substract_quarters(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+--------------+---------+------------+ +| | a | b | Output | ++--------+--------------+---------+------------+ +| Type | Date | Int32 | Date | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-09-23 | 1 | 1969-06-23 | +| Row 1 | 1970-01-01 | 2 | 1969-07-01 | +| Row 2 | 1970-04-11 | 3 | 1969-07-11 | ++--------+--------------+---------+------------+ +evaluation (internal): ++--------+--------------------+ +| Column | Data | ++--------+--------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [-192, -184, -174] | ++--------+--------------------+ + + +ast : date_sub(month, b, a) +raw expr : substract_months(ColumnRef(0)::Date, ColumnRef(1)::Int32) +checked expr : substract_months(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+--------------+---------+------------+ +| | a | b | Output | ++--------+--------------+---------+------------+ +| Type | Date | Int32 | Date | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-09-23 | 1 | 1969-08-23 | +| Row 1 | 1970-01-01 | 2 | 1969-11-01 | +| Row 2 | 1970-04-11 | 3 | 1970-01-11 | ++--------+--------------+---------+------------+ +evaluation (internal): ++--------+------------------+ +| Column | Data | ++--------+------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [-131, -61, 10] | ++--------+------------------+ + + +ast : date_sub(day, b, a) +raw expr : substract_days(ColumnRef(0)::Date, ColumnRef(1)::Int32) +checked expr : substract_days(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+--------------+---------+------------+ +| | a | b | Output | ++--------+--------------+---------+------------+ +| Type | Date | Int32 | Date | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-09-23 | 1 | 1969-09-22 | +| Row 1 | 1970-01-01 | 2 | 1969-12-30 | +| Row 2 | 1970-04-11 | 3 | 1970-04-08 | ++--------+--------------+---------+------------+ +evaluation (internal): ++--------+------------------+ +| Column | Data | ++--------+------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [-101, -2, 97] | ++--------+------------------+ + + +error: + --> SQL:1:1 + | +1 | date_add(year, 10000, to_timestamp(0)) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ timestamp `315569520000000000` is out of range + + + +ast : date_add(year, 100, to_timestamp(0)) +raw expr : add_years(to_timestamp(0_u8), 100_u8) +checked expr : add_years(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : 3155760000000000 +output type : Timestamp +output domain : Unknown +output : 2070-01-01 00:00:00.000000 + + +ast : date_add(quarter, 100, to_timestamp(0)) +raw expr : add_quarters(to_timestamp(0_u8), 100_u8) +checked expr : add_quarters(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : 788918400000000 +output type : Timestamp +output domain : Unknown +output : 1995-01-01 00:00:00.000000 + + +ast : date_add(month, 100, to_timestamp(0)) +raw expr : add_months(to_timestamp(0_u8), 100_u8) +checked expr : add_months(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : 262828800000000 +output type : Timestamp +output domain : Unknown +output : 1978-05-01 00:00:00.000000 + + +ast : date_add(day, 100, to_timestamp(0)) +raw expr : add_days(to_timestamp(0_u8), 100_u8) +checked expr : add_days(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : 8640000000000 +output type : Timestamp +output domain : Unknown +output : 1970-04-11 00:00:00.000000 + + +ast : date_add(hour, 100, to_timestamp(0)) +raw expr : add_hours(to_timestamp(0_u8), 100_u8) +checked expr : add_hours(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : 360000000000 +output type : Timestamp +output domain : Unknown +output : 1970-01-05 04:00:00.000000 + + +ast : date_add(minute, 100, to_timestamp(0)) +raw expr : add_minutes(to_timestamp(0_u8), 100_u8) +checked expr : add_minutes(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : 6000000000 +output type : Timestamp +output domain : Unknown +output : 1970-01-01 01:40:00.000000 + + +ast : date_add(second, 100, to_timestamp(0)) +raw expr : add_seconds(to_timestamp(0_u8), 100_u8) +checked expr : add_seconds(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : 100000000 +output type : Timestamp +output domain : Unknown +output : 1970-01-01 00:01:40.000000 + + +ast : date_sub(year, 100, to_timestamp(0)) +raw expr : substract_years(to_timestamp(0_u8), 100_u8) +checked expr : substract_years(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : -3155673600000000 +output type : Timestamp +output domain : Unknown +output : 1870-01-01 00:00:00.000000 + + +ast : date_sub(quarter, 100, to_timestamp(0)) +raw expr : substract_quarters(to_timestamp(0_u8), 100_u8) +checked expr : substract_quarters(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : -788918400000000 +output type : Timestamp +output domain : Unknown +output : 1945-01-01 00:00:00.000000 + + +ast : date_sub(month, 100, to_timestamp(0)) +raw expr : substract_months(to_timestamp(0_u8), 100_u8) +checked expr : substract_months(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : -263001600000000 +output type : Timestamp +output domain : Unknown +output : 1961-09-01 00:00:00.000000 + + +ast : date_sub(day, 100, to_timestamp(0)) +raw expr : substract_days(to_timestamp(0_u8), 100_u8) +checked expr : substract_days(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : -8640000000000 +output type : Timestamp +output domain : Unknown +output : 1969-09-23 00:00:00.000000 + + +ast : date_sub(hour, 100, to_timestamp(0)) +raw expr : substract_hours(to_timestamp(0_u8), 100_u8) +checked expr : substract_hours(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : -360000000000 +output type : Timestamp +output domain : Unknown +output : 1969-12-27 20:00:00.000000 + + +ast : date_sub(minute, 100, to_timestamp(0)) +raw expr : substract_minutes(to_timestamp(0_u8), 100_u8) +checked expr : substract_minutes(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : -6000000000 +output type : Timestamp +output domain : Unknown +output : 1969-12-31 22:20:00.000000 + + +ast : date_sub(second, 100, to_timestamp(0)) +raw expr : substract_seconds(to_timestamp(0_u8), 100_u8) +checked expr : substract_seconds(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : -100000000 +output type : Timestamp +output domain : Unknown +output : 1969-12-31 23:58:20.000000 + + +ast : date_add(year, b, a) +raw expr : add_years(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) +checked expr : add_years(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+----------------------------+---------+----------------------------+ +| | a | b | Output | ++--------+----------------------------+---------+----------------------------+ +| Type | Timestamp | Int32 | Timestamp | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-12-31 23:59:59.999900 | 1 | 1970-12-31 23:59:59.999900 | +| Row 1 | 1970-01-01 00:00:00.000000 | 2 | 1972-01-01 00:00:00.000000 | +| Row 2 | 1970-01-01 00:00:00.000100 | 3 | 1973-01-01 00:00:00.000100 | ++--------+----------------------------+---------+----------------------------+ +evaluation (internal): ++--------+--------------------------------------------------+ +| Column | Data | ++--------+--------------------------------------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [31535999999900, 63072000000000, 94694400000100] | ++--------+--------------------------------------------------+ + + +ast : date_add(quarter, b, a) +raw expr : add_quarters(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) +checked expr : add_quarters(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+----------------------------+---------+----------------------------+ +| | a | b | Output | ++--------+----------------------------+---------+----------------------------+ +| Type | Timestamp | Int32 | Timestamp | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-12-31 23:59:59.999900 | 1 | 1970-03-31 23:59:59.999900 | +| Row 1 | 1970-01-01 00:00:00.000000 | 2 | 1970-07-01 00:00:00.000000 | +| Row 2 | 1970-01-01 00:00:00.000100 | 3 | 1970-10-01 00:00:00.000100 | ++--------+----------------------------+---------+----------------------------+ +evaluation (internal): ++--------+-------------------------------------------------+ +| Column | Data | ++--------+-------------------------------------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [7775999999900, 15638400000000, 23587200000100] | ++--------+-------------------------------------------------+ + + +ast : date_add(month, b, a) +raw expr : add_months(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) +checked expr : add_months(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+----------------------------+---------+----------------------------+ +| | a | b | Output | ++--------+----------------------------+---------+----------------------------+ +| Type | Timestamp | Int32 | Timestamp | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-12-31 23:59:59.999900 | 1 | 1970-01-31 23:59:59.999900 | +| Row 1 | 1970-01-01 00:00:00.000000 | 2 | 1970-03-01 00:00:00.000000 | +| Row 2 | 1970-01-01 00:00:00.000100 | 3 | 1970-04-01 00:00:00.000100 | ++--------+----------------------------+---------+----------------------------+ +evaluation (internal): ++--------+-----------------------------------------------+ +| Column | Data | ++--------+-----------------------------------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [2678399999900, 5097600000000, 7776000000100] | ++--------+-----------------------------------------------+ + + +ast : date_add(day, b, a) +raw expr : add_days(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) +checked expr : add_days(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+----------------------------+---------+----------------------------+ +| | a | b | Output | ++--------+----------------------------+---------+----------------------------+ +| Type | Timestamp | Int32 | Timestamp | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-12-31 23:59:59.999900 | 1 | 1970-01-01 23:59:59.999900 | +| Row 1 | 1970-01-01 00:00:00.000000 | 2 | 1970-01-03 00:00:00.000000 | +| Row 2 | 1970-01-01 00:00:00.000100 | 3 | 1970-01-04 00:00:00.000100 | ++--------+----------------------------+---------+----------------------------+ +evaluation (internal): ++--------+-------------------------------------------+ +| Column | Data | ++--------+-------------------------------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [86399999900, 172800000000, 259200000100] | ++--------+-------------------------------------------+ + + +ast : date_add(hour, b, a) +raw expr : add_hours(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) +checked expr : add_hours(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+----------------------------+---------+----------------------------+ +| | a | b | Output | ++--------+----------------------------+---------+----------------------------+ +| Type | Timestamp | Int32 | Timestamp | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-12-31 23:59:59.999900 | 1 | 1970-01-01 00:59:59.999900 | +| Row 1 | 1970-01-01 00:00:00.000000 | 2 | 1970-01-01 02:00:00.000000 | +| Row 2 | 1970-01-01 00:00:00.000100 | 3 | 1970-01-01 03:00:00.000100 | ++--------+----------------------------+---------+----------------------------+ +evaluation (internal): ++--------+---------------------------------------+ +| Column | Data | ++--------+---------------------------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [3599999900, 7200000000, 10800000100] | ++--------+---------------------------------------+ + + +ast : date_add(minute, b, a) +raw expr : add_minutes(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) +checked expr : add_minutes(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+----------------------------+---------+----------------------------+ +| | a | b | Output | ++--------+----------------------------+---------+----------------------------+ +| Type | Timestamp | Int32 | Timestamp | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-12-31 23:59:59.999900 | 1 | 1970-01-01 00:00:59.999900 | +| Row 1 | 1970-01-01 00:00:00.000000 | 2 | 1970-01-01 00:02:00.000000 | +| Row 2 | 1970-01-01 00:00:00.000100 | 3 | 1970-01-01 00:03:00.000100 | ++--------+----------------------------+---------+----------------------------+ +evaluation (internal): ++--------+----------------------------------+ +| Column | Data | ++--------+----------------------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [59999900, 120000000, 180000100] | ++--------+----------------------------------+ + + +ast : date_add(second, b, a) +raw expr : add_seconds(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) +checked expr : add_seconds(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+----------------------------+---------+----------------------------+ +| | a | b | Output | ++--------+----------------------------+---------+----------------------------+ +| Type | Timestamp | Int32 | Timestamp | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-12-31 23:59:59.999900 | 1 | 1970-01-01 00:00:00.999900 | +| Row 1 | 1970-01-01 00:00:00.000000 | 2 | 1970-01-01 00:00:02.000000 | +| Row 2 | 1970-01-01 00:00:00.000100 | 3 | 1970-01-01 00:00:03.000100 | ++--------+----------------------------+---------+----------------------------+ +evaluation (internal): ++--------+----------------------------+ +| Column | Data | ++--------+----------------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [999900, 2000000, 3000100] | ++--------+----------------------------+ + + +ast : date_sub(year, b, a) +raw expr : substract_years(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) +checked expr : substract_years(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+----------------------------+---------+----------------------------+ +| | a | b | Output | ++--------+----------------------------+---------+----------------------------+ +| Type | Timestamp | Int32 | Timestamp | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-12-31 23:59:59.999900 | 1 | 1968-12-31 23:59:59.999900 | +| Row 1 | 1970-01-01 00:00:00.000000 | 2 | 1968-01-01 00:00:00.000000 | +| Row 2 | 1970-01-01 00:00:00.000100 | 3 | 1967-01-01 00:00:00.000100 | ++--------+----------------------------+---------+----------------------------+ +evaluation (internal): ++--------+-----------------------------------------------------+ +| Column | Data | ++--------+-----------------------------------------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [-31536000000100, -63158400000000, -94694399999900] | ++--------+-----------------------------------------------------+ + + +ast : date_sub(quarter, b, a) +raw expr : substract_quarters(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) +checked expr : substract_quarters(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+----------------------------+---------+----------------------------+ +| | a | b | Output | ++--------+----------------------------+---------+----------------------------+ +| Type | Timestamp | Int32 | Timestamp | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-12-31 23:59:59.999900 | 1 | 1969-09-30 23:59:59.999900 | +| Row 1 | 1970-01-01 00:00:00.000000 | 2 | 1969-07-01 00:00:00.000000 | +| Row 2 | 1970-01-01 00:00:00.000100 | 3 | 1969-04-01 00:00:00.000100 | ++--------+----------------------------+---------+----------------------------+ +evaluation (internal): ++--------+----------------------------------------------------+ +| Column | Data | ++--------+----------------------------------------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [-7948800000100, -15897600000000, -23759999999900] | ++--------+----------------------------------------------------+ + + +ast : date_sub(month, b, a) +raw expr : substract_months(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) +checked expr : substract_months(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+----------------------------+---------+----------------------------+ +| | a | b | Output | ++--------+----------------------------+---------+----------------------------+ +| Type | Timestamp | Int32 | Timestamp | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-12-31 23:59:59.999900 | 1 | 1969-11-30 23:59:59.999900 | +| Row 1 | 1970-01-01 00:00:00.000000 | 2 | 1969-11-01 00:00:00.000000 | +| Row 2 | 1970-01-01 00:00:00.000100 | 3 | 1969-10-01 00:00:00.000100 | ++--------+----------------------------+---------+----------------------------+ +evaluation (internal): ++--------+--------------------------------------------------+ +| Column | Data | ++--------+--------------------------------------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [-2678400000100, -5270400000000, -7948799999900] | ++--------+--------------------------------------------------+ + + +ast : date_sub(day, b, a) +raw expr : substract_days(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) +checked expr : substract_days(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+----------------------------+---------+----------------------------+ +| | a | b | Output | ++--------+----------------------------+---------+----------------------------+ +| Type | Timestamp | Int32 | Timestamp | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-12-31 23:59:59.999900 | 1 | 1969-12-30 23:59:59.999900 | +| Row 1 | 1970-01-01 00:00:00.000000 | 2 | 1969-12-30 00:00:00.000000 | +| Row 2 | 1970-01-01 00:00:00.000100 | 3 | 1969-12-29 00:00:00.000100 | ++--------+----------------------------+---------+----------------------------+ +evaluation (internal): ++--------+----------------------------------------------+ +| Column | Data | ++--------+----------------------------------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [-86400000100, -172800000000, -259199999900] | ++--------+----------------------------------------------+ + + +ast : date_sub(hour, b, a) +raw expr : substract_hours(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) +checked expr : substract_hours(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+----------------------------+---------+----------------------------+ +| | a | b | Output | ++--------+----------------------------+---------+----------------------------+ +| Type | Timestamp | Int32 | Timestamp | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-12-31 23:59:59.999900 | 1 | 1969-12-31 22:59:59.999900 | +| Row 1 | 1970-01-01 00:00:00.000000 | 2 | 1969-12-31 22:00:00.000000 | +| Row 2 | 1970-01-01 00:00:00.000100 | 3 | 1969-12-31 21:00:00.000100 | ++--------+----------------------------+---------+----------------------------+ +evaluation (internal): ++--------+------------------------------------------+ +| Column | Data | ++--------+------------------------------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [-3600000100, -7200000000, -10799999900] | ++--------+------------------------------------------+ + + +ast : date_sub(minute, b, a) +raw expr : substract_minutes(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) +checked expr : substract_minutes(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+----------------------------+---------+----------------------------+ +| | a | b | Output | ++--------+----------------------------+---------+----------------------------+ +| Type | Timestamp | Int32 | Timestamp | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-12-31 23:59:59.999900 | 1 | 1969-12-31 23:58:59.999900 | +| Row 1 | 1970-01-01 00:00:00.000000 | 2 | 1969-12-31 23:58:00.000000 | +| Row 2 | 1970-01-01 00:00:00.000100 | 3 | 1969-12-31 23:57:00.000100 | ++--------+----------------------------+---------+----------------------------+ +evaluation (internal): ++--------+-------------------------------------+ +| Column | Data | ++--------+-------------------------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [-60000100, -120000000, -179999900] | ++--------+-------------------------------------+ + + +ast : date_sub(second, b, a) +raw expr : substract_seconds(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) +checked expr : substract_seconds(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+----------------------------+---------+----------------------------+ +| | a | b | Output | ++--------+----------------------------+---------+----------------------------+ +| Type | Timestamp | Int32 | Timestamp | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-12-31 23:59:59.999900 | 1 | 1969-12-31 23:59:58.999900 | +| Row 1 | 1970-01-01 00:00:00.000000 | 2 | 1969-12-31 23:59:58.000000 | +| Row 2 | 1970-01-01 00:00:00.000100 | 3 | 1969-12-31 23:59:57.000100 | ++--------+----------------------------+---------+----------------------------+ +evaluation (internal): ++--------+--------------------------------+ +| Column | Data | ++--------+--------------------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [-1000100, -2000000, -2999900] | ++--------+--------------------------------+ + + From 32d36ca15886f8ac432bc43cfd3bb5725d86c2ad Mon Sep 17 00:00:00 2001 From: RinChanNOWWW Date: Fri, 14 Oct 2022 17:40:47 +0800 Subject: [PATCH 4/8] Remove `IntervalType` in the new expression framework. --- src/query/expression/src/converts/from.rs | 8 +- src/query/expression/src/converts/to.rs | 2 +- src/query/expression/src/display.rs | 5 - src/query/expression/src/evaluator.rs | 15 +- src/query/expression/src/kernels/concat.rs | 5 - src/query/expression/src/kernels/filter.rs | 4 - src/query/expression/src/kernels/scatter.rs | 7 - src/query/expression/src/kernels/take.rs | 8 - .../expression/src/kernels/take_chunks.rs | 7 - src/query/expression/src/property.rs | 8 - src/query/expression/src/types.rs | 3 - src/query/expression/src/types/interval.rs | 170 ------------------ src/query/expression/src/types/variant.rs | 1 - src/query/expression/src/values.rs | 55 ------ .../src/aggregates/aggregate_scalar_state.rs | 1 - 15 files changed, 8 insertions(+), 291 deletions(-) delete mode 100644 src/query/expression/src/types/interval.rs diff --git a/src/query/expression/src/converts/from.rs b/src/query/expression/src/converts/from.rs index 27f150826282..0e4bdd3c7954 100644 --- a/src/query/expression/src/converts/from.rs +++ b/src/query/expression/src/converts/from.rs @@ -32,10 +32,7 @@ use crate::Value; pub fn can_convert(datatype: &DataTypeImpl) -> bool { !matches!( datatype, - DataTypeImpl::Date(_) - | DataTypeImpl::Interval(_) - | DataTypeImpl::VariantArray(_) - | DataTypeImpl::VariantObject(_) + DataTypeImpl::Date(_) | DataTypeImpl::VariantArray(_) | DataTypeImpl::VariantObject(_) ) } @@ -48,7 +45,6 @@ pub fn from_type(datatype: &DataTypeImpl) -> DataType { DataTypeImpl::Boolean(_) => DataType::Boolean, DataTypeImpl::Timestamp(_) => DataType::Timestamp, DataTypeImpl::Date(_) => DataType::Date, - DataTypeImpl::Interval(_) => DataType::Interval, DataTypeImpl::String(_) => DataType::String, DataTypeImpl::Struct(ty) => { let inners = ty.types().iter().map(from_type).collect(); @@ -58,6 +54,7 @@ pub fn from_type(datatype: &DataTypeImpl) -> DataType { DataTypeImpl::Variant(_) | DataTypeImpl::VariantArray(_) | DataTypeImpl::VariantObject(_) => DataType::Variant, + DataTypeImpl::Interval(_) => unimplemented!(), }) } @@ -102,7 +99,6 @@ pub fn from_scalar(datavalue: &DataValue, datatype: &DataTypeImpl) -> Scalar { } DataTypeImpl::Timestamp(_) => Scalar::Timestamp(datavalue.as_i64().unwrap() as i64), DataTypeImpl::Date(_) => Scalar::Date(datavalue.as_i64().unwrap() as i32), - DataTypeImpl::Interval(_) => Scalar::Interval(datavalue.as_i64().unwrap() as i64), DataTypeImpl::String(_) => Scalar::String(datavalue.as_string().unwrap()), DataTypeImpl::Struct(types) => { let values = match datavalue { diff --git a/src/query/expression/src/converts/to.rs b/src/query/expression/src/converts/to.rs index e881d41f91ee..aa3015a2f746 100644 --- a/src/query/expression/src/converts/to.rs +++ b/src/query/expression/src/converts/to.rs @@ -38,7 +38,6 @@ pub fn to_type(datatype: &DataTypeImpl) -> DataType { DataTypeImpl::Boolean(_) => DataType::Boolean, DataTypeImpl::Timestamp(_) => DataType::Timestamp, DataTypeImpl::Date(_) => DataType::Date, - DataTypeImpl::Interval(_) => DataType::Interval, DataTypeImpl::String(_) => DataType::String, DataTypeImpl::Struct(ty) => { let inners = ty.types().iter().map(to_type).collect(); @@ -48,6 +47,7 @@ pub fn to_type(datatype: &DataTypeImpl) -> DataType { DataTypeImpl::Variant(_) | DataTypeImpl::VariantArray(_) | DataTypeImpl::VariantObject(_) => DataType::Variant, + DataTypeImpl::Interval(_) => unimplemented!(), }) } diff --git a/src/query/expression/src/display.rs b/src/query/expression/src/display.rs index 20e8b8decff6..d5528c6d1766 100755 --- a/src/query/expression/src/display.rs +++ b/src/query/expression/src/display.rs @@ -97,7 +97,6 @@ impl<'a> Debug for ScalarRef<'a> { ScalarRef::String(s) => write!(f, "{:?}", String::from_utf8_lossy(s)), ScalarRef::Timestamp(t) => write!(f, "{t:?}"), ScalarRef::Date(d) => write!(f, "{d:?}"), - ScalarRef::Interval(i) => write!(f, "{i:?}"), ScalarRef::Array(col) => write!(f, "[{}]", col.iter().join(", ")), ScalarRef::Tuple(fields) => { write!( @@ -121,7 +120,6 @@ impl Debug for Column { Column::String(col) => write!(f, "{col:?}"), Column::Timestamp(col) => write!(f, "{col:?}"), Column::Date(col) => write!(f, "{col:?}"), - Column::Interval(col) => write!(f, "{col:?}"), Column::Array(col) => write!(f, "{col:?}"), Column::Nullable(col) => write!(f, "{col:?}"), Column::Tuple { fields, len } => f @@ -144,7 +142,6 @@ impl<'a> Display for ScalarRef<'a> { ScalarRef::String(s) => write!(f, "{:?}", String::from_utf8_lossy(s)), ScalarRef::Timestamp(t) => write!(f, "{}", display_timestamp(*t)), ScalarRef::Date(d) => write!(f, "{}", display_date(*d as i64)), - ScalarRef::Interval(i) => write!(f, "{}", display_timestamp(*i)), ScalarRef::Array(col) => write!(f, "[{}]", col.iter().join(", ")), ScalarRef::Tuple(fields) => { write!( @@ -357,7 +354,6 @@ impl Display for DataType { DataType::Number(num) => write!(f, "{num}"), DataType::Timestamp => write!(f, "Timestamp"), DataType::Date => write!(f, "Date"), - DataType::Interval => write!(f, "Interval"), DataType::Null => write!(f, "NULL"), DataType::Nullable(inner) => write!(f, "{inner} NULL"), DataType::EmptyArray => write!(f, "Array(Nothing)"), @@ -565,7 +561,6 @@ impl Display for Domain { Domain::String(domain) => write!(f, "{domain}"), Domain::Timestamp(domain) => write!(f, "{domain}"), Domain::Date(domain) => write!(f, "{domain}"), - Domain::Interval(domain) => write!(f, "{domain}"), Domain::Nullable(domain) => write!(f, "{domain}"), Domain::Array(None) => write!(f, "[]"), Domain::Array(Some(domain)) => write!(f, "[{domain}]"), diff --git a/src/query/expression/src/evaluator.rs b/src/query/expression/src/evaluator.rs index 6d0a1689322f..a53032a18ddc 100644 --- a/src/query/expression/src/evaluator.rs +++ b/src/query/expression/src/evaluator.rs @@ -263,8 +263,7 @@ impl<'a> Evaluator<'a> { | (scalar @ Scalar::Boolean(_), DataType::Boolean) | (scalar @ Scalar::String(_), DataType::String) | (scalar @ Scalar::Timestamp(_), DataType::Timestamp) - | (scalar @ Scalar::Date(_), DataType::Date) - | (scalar @ Scalar::Interval(_), DataType::Interval) => Ok(scalar), + | (scalar @ Scalar::Date(_), DataType::Date) => Ok(scalar), (scalar, dest_ty) => Err(( span, @@ -440,8 +439,7 @@ impl<'a> Evaluator<'a> { | (col @ Column::Boolean(_), DataType::Boolean) | (col @ Column::String { .. }, DataType::String) | (col @ Column::Timestamp { .. }, DataType::Timestamp) - | (col @ Column::Date(_), DataType::Date) - | (col @ Column::Interval(_), DataType::Interval) => Ok(col), + | (col @ Column::Date(_), DataType::Date) => Ok(col), (col, dest_ty) => Err((span, (format!("unable to cast {col:?} to {dest_ty}")))), } @@ -655,8 +653,7 @@ impl<'a> Evaluator<'a> { | (column @ Column::String { .. }, DataType::String) | (column @ Column::EmptyArray { .. }, DataType::EmptyArray) | (column @ Column::Timestamp { .. }, DataType::Timestamp) - | (column @ Column::Date(_), DataType::Date) - | (column @ Column::Interval(_), DataType::Interval) => { + | (column @ Column::Date(_), DataType::Date) => { Column::Nullable(Box::new(NullableColumn { validity: constant_bitmap(true, column.len()).into(), column, @@ -933,8 +930,7 @@ impl<'a> ConstantFolder<'a> { (Domain::Boolean(_), DataType::Boolean) | (Domain::String(_), DataType::String) | (Domain::Timestamp(_), DataType::Timestamp) - | (Domain::Date(_), DataType::Date) - | (Domain::Interval(_), DataType::Interval) => Some(domain.clone()), + | (Domain::Date(_), DataType::Date) => Some(domain.clone()), // failure cases _ => None, @@ -1059,8 +1055,7 @@ impl<'a> ConstantFolder<'a> { (Domain::Boolean(_), DataType::Boolean) | (Domain::String(_), DataType::String) | (Domain::Timestamp(_), DataType::Timestamp) - | (Domain::Date(_), DataType::Date) - | (Domain::Interval(_), DataType::Interval) => Domain::Nullable(NullableDomain { + | (Domain::Date(_), DataType::Date) => Domain::Nullable(NullableDomain { has_null: false, value: Some(Box::new(domain.clone())), }), diff --git a/src/query/expression/src/kernels/concat.rs b/src/query/expression/src/kernels/concat.rs index 5194cb3b4eb1..3cef7f097e28 100644 --- a/src/query/expression/src/kernels/concat.rs +++ b/src/query/expression/src/kernels/concat.rs @@ -25,7 +25,6 @@ use crate::types::ArrayType; use crate::types::BooleanType; use crate::types::DateType; use crate::types::EmptyArrayType; -use crate::types::IntervalType; use crate::types::NullType; use crate::types::NullableType; use crate::types::NumberType; @@ -101,10 +100,6 @@ impl Column { let builder = Vec::with_capacity(capacity); Self::concat_value_types::(builder, columns) } - Column::Interval(_) => { - let builder = Vec::with_capacity(capacity); - Self::concat_value_types::(builder, columns) - } Column::Array(col) => { let mut builder = ArrayColumnBuilder::::from_column(col.slice(0..0)); builder.reserve(capacity); diff --git a/src/query/expression/src/kernels/filter.rs b/src/query/expression/src/kernels/filter.rs index 0449c03cf3e3..f26f4b884ac7 100644 --- a/src/query/expression/src/kernels/filter.rs +++ b/src/query/expression/src/kernels/filter.rs @@ -171,10 +171,6 @@ impl Column { let d = Self::filter_primitive_types(column, filter); Column::Date(d) } - Column::Interval(column) => { - let i = Self::filter_primitive_types(column, filter); - Column::Interval(i) - } Column::Array(column) => { let mut builder = ArrayColumnBuilder::::from_column(column.slice(0..0)); builder.reserve(length); diff --git a/src/query/expression/src/kernels/scatter.rs b/src/query/expression/src/kernels/scatter.rs index ad0bfeeb5a22..b386f21292e8 100644 --- a/src/query/expression/src/kernels/scatter.rs +++ b/src/query/expression/src/kernels/scatter.rs @@ -25,7 +25,6 @@ use crate::types::ArrayType; use crate::types::BooleanType; use crate::types::DataType; use crate::types::DateType; -use crate::types::IntervalType; use crate::types::NumberType; use crate::types::StringType; use crate::types::TimestampType; @@ -137,12 +136,6 @@ impl Column { indices, scatter_size, ), - Column::Interval(column) => Self::scatter_scalars::( - column, - Vec::with_capacity(length), - indices, - scatter_size, - ), Column::Array(column) => { let mut builder = ArrayColumnBuilder::::from_column(column.slice(0..0)); builder.reserve(length); diff --git a/src/query/expression/src/kernels/take.rs b/src/query/expression/src/kernels/take.rs index 0505eee611b8..90368bdb6c9e 100644 --- a/src/query/expression/src/kernels/take.rs +++ b/src/query/expression/src/kernels/take.rs @@ -77,14 +77,6 @@ impl Column { .unwrap(); Column::Date(d) } - Column::Interval(column) => { - let i = Self::take_arg_types::, _>(column, indices) - .into_number() - .unwrap() - .into_int64() - .unwrap(); - Column::Interval(i) - } Column::Array(column) => { let mut builder = ArrayColumnBuilder::::from_column(column.slice(0..0)); builder.reserve(length); diff --git a/src/query/expression/src/kernels/take_chunks.rs b/src/query/expression/src/kernels/take_chunks.rs index db9cb34ae474..9bbd087f4929 100644 --- a/src/query/expression/src/kernels/take_chunks.rs +++ b/src/query/expression/src/kernels/take_chunks.rs @@ -21,7 +21,6 @@ use crate::types::ArrayType; use crate::types::BooleanType; use crate::types::DataType; use crate::types::DateType; -use crate::types::IntervalType; use crate::types::NumberType; use crate::types::StringType; use crate::types::TimestampType; @@ -128,16 +127,10 @@ impl Column { let builder = TimestampType::create_builder(result_size, &[]); Self::take_chunk_value_types::(columns, builder, indices) } - Column::Date(_) => { let builder = DateType::create_builder(result_size, &[]); Self::take_chunk_value_types::(columns, builder, indices) } - Column::Interval(_) => { - let builder = IntervalType::create_builder(result_size, &[]); - Self::take_chunk_value_types::(columns, builder, indices) - } - Column::Array(column) => { let mut builder = ArrayColumnBuilder::::from_column(column.slice(0..0)); builder.reserve(result_size); diff --git a/src/query/expression/src/property.rs b/src/query/expression/src/property.rs index 9cebe4dceb87..2fdb2444cc81 100644 --- a/src/query/expression/src/property.rs +++ b/src/query/expression/src/property.rs @@ -43,7 +43,6 @@ pub enum Domain { String(StringDomain), Timestamp(SimpleDomain), Date(SimpleDomain), - Interval(SimpleDomain), Nullable(NullableDomain), Array(Option>), Tuple(Vec), @@ -85,10 +84,6 @@ impl Domain { min: this.min.min(other.min), max: this.max.max(other.max), }), - (Domain::Interval(this), Domain::Interval(other)) => Domain::Interval(SimpleDomain { - min: this.min.min(other.min), - max: this.max.max(other.max), - }), ( Domain::Nullable(NullableDomain { has_null: true, @@ -206,9 +201,6 @@ impl Domain { Some(Scalar::Timestamp(*min)) } Domain::Date(SimpleDomain { min, max }) if min == max => Some(Scalar::Date(*min)), - Domain::Interval(SimpleDomain { min, max }) if min == max => { - Some(Scalar::Interval(*min)) - } Domain::Nullable(NullableDomain { has_null: true, value: None, diff --git a/src/query/expression/src/types.rs b/src/query/expression/src/types.rs index b280e2415166..7cea42c1f6a4 100755 --- a/src/query/expression/src/types.rs +++ b/src/query/expression/src/types.rs @@ -19,7 +19,6 @@ pub mod boolean; pub mod date; pub mod empty_array; pub mod generic; -pub mod interval; pub mod map; pub mod null; pub mod nullable; @@ -42,7 +41,6 @@ pub use self::boolean::BooleanType; pub use self::date::DateType; pub use self::empty_array::EmptyArrayType; pub use self::generic::GenericType; -pub use self::interval::IntervalType; pub use self::map::MapType; pub use self::null::NullType; pub use self::nullable::NullableType; @@ -67,7 +65,6 @@ pub enum DataType { Number(NumberDataType), Timestamp, Date, - Interval, Null, Nullable(Box), EmptyArray, diff --git a/src/query/expression/src/types/interval.rs b/src/query/expression/src/types/interval.rs deleted file mode 100644 index 1b4566bfda40..000000000000 --- a/src/query/expression/src/types/interval.rs +++ /dev/null @@ -1,170 +0,0 @@ -// Copyright 2022 Datafuse Labs. -// -// 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::ops::Range; - -use common_arrow::arrow::buffer::Buffer; - -use super::number::SimpleDomain; -use crate::property::Domain; -use crate::types::ArgType; -use crate::types::DataType; -use crate::types::GenericMap; -use crate::types::ValueType; -use crate::util::buffer_into_mut; -use crate::values::Column; -use crate::values::Scalar; -use crate::ColumnBuilder; -use crate::ScalarRef; - -#[derive(Debug, Clone, PartialEq, Eq)] -pub struct IntervalType; - -impl ValueType for IntervalType { - type Scalar = i64; - type ScalarRef<'a> = i64; - type Column = Buffer; - type Domain = SimpleDomain; - type ColumnIterator<'a> = std::iter::Cloned>; - type ColumnBuilder = Vec; - - #[inline] - fn upcast_gat<'short, 'long: 'short>(long: i64) -> i64 { - long - } - - fn to_owned_scalar<'a>(scalar: Self::ScalarRef<'a>) -> Self::Scalar { - scalar - } - - fn to_scalar_ref<'a>(scalar: &'a Self::Scalar) -> Self::ScalarRef<'a> { - *scalar - } - - fn try_downcast_scalar<'a>(scalar: &'a ScalarRef) -> Option> { - match scalar { - ScalarRef::Interval(scalar) => Some(*scalar), - _ => None, - } - } - - fn try_downcast_column<'a>(col: &'a Column) -> Option { - match col { - Column::Interval(column) => Some(column.clone()), - _ => None, - } - } - - fn try_downcast_domain(domain: &Domain) -> Option> { - domain.as_interval().map(SimpleDomain::clone) - } - - fn try_downcast_builder<'a>( - builder: &'a mut ColumnBuilder, - ) -> Option<&'a mut Self::ColumnBuilder> { - match builder { - ColumnBuilder::Interval(builder) => Some(builder), - _ => None, - } - } - - fn upcast_scalar(scalar: Self::Scalar) -> Scalar { - Scalar::Interval(scalar) - } - - fn upcast_column(col: Self::Column) -> Column { - Column::Interval(col) - } - - fn upcast_domain(domain: SimpleDomain) -> Domain { - Domain::Interval(domain) - } - - fn column_len<'a>(col: &'a Self::Column) -> usize { - col.len() - } - - fn index_column<'a>(col: &'a Self::Column, index: usize) -> Option> { - col.get(index).cloned() - } - - unsafe fn index_column_unchecked<'a>( - col: &'a Self::Column, - index: usize, - ) -> Self::ScalarRef<'a> { - *col.get_unchecked(index) - } - - fn slice_column<'a>(col: &'a Self::Column, range: Range) -> Self::Column { - col.clone().slice(range.start, range.end - range.start) - } - - fn iter_column<'a>(col: &'a Self::Column) -> Self::ColumnIterator<'a> { - col.iter().cloned() - } - - fn column_to_builder(col: Self::Column) -> Self::ColumnBuilder { - buffer_into_mut(col) - } - - fn builder_len(builder: &Self::ColumnBuilder) -> usize { - builder.len() - } - - fn push_item(builder: &mut Self::ColumnBuilder, item: Self::Scalar) { - builder.push(item); - } - - fn push_default(builder: &mut Self::ColumnBuilder) { - builder.push(Self::Scalar::default()); - } - - fn append_builder(builder: &mut Self::ColumnBuilder, other: &Self::ColumnBuilder) { - builder.extend_from_slice(other); - } - - fn build_column(builder: Self::ColumnBuilder) -> Self::Column { - builder.into() - } - - fn build_scalar(builder: Self::ColumnBuilder) -> Self::Scalar { - assert_eq!(builder.len(), 1); - builder[0] - } -} - -impl ArgType for IntervalType { - fn data_type() -> DataType { - DataType::Interval - } - - fn create_builder(capacity: usize, _generics: &GenericMap) -> Self::ColumnBuilder { - Vec::with_capacity(capacity) - } - - fn column_from_vec(vec: Vec, _generics: &GenericMap) -> Self::Column { - vec.into() - } - - fn column_from_iter(iter: impl Iterator, _: &GenericMap) -> Self::Column { - iter.collect() - } - - fn column_from_ref_iter<'a>( - iter: impl Iterator>, - _: &GenericMap, - ) -> Self::Column { - iter.collect() - } -} diff --git a/src/query/expression/src/types/variant.rs b/src/query/expression/src/types/variant.rs index 7151e6e5339e..181a1f4838d1 100644 --- a/src/query/expression/src/types/variant.rs +++ b/src/query/expression/src/types/variant.rs @@ -176,7 +176,6 @@ pub fn cast_scalar_to_variant(scalar: ScalarRef, buf: &mut Vec) { ScalarRef::String(s) => common_jsonb::Value::String(String::from_utf8_lossy(s)), ScalarRef::Timestamp(ts) => ts.into(), ScalarRef::Date(d) => d.into(), - ScalarRef::Interval(i) => i.into(), ScalarRef::Array(col) => { let items = cast_scalars_to_variants(col.iter()); common_jsonb::build_array(items.iter(), buf).expect("failed to build jsonb array"); diff --git a/src/query/expression/src/values.rs b/src/query/expression/src/values.rs index a7e35aad2d3e..3a76e21da02f 100755 --- a/src/query/expression/src/values.rs +++ b/src/query/expression/src/values.rs @@ -75,7 +75,6 @@ pub enum Scalar { Number(NumberScalar), Timestamp(i64), Date(i32), - Interval(i64), Boolean(bool), String(Vec), Array(Column), @@ -93,7 +92,6 @@ pub enum ScalarRef<'a> { String(&'a [u8]), Timestamp(i64), Date(i32), - Interval(i64), Array(Column), Tuple(Vec>), Variant(&'a [u8]), @@ -108,7 +106,6 @@ pub enum Column { String(StringColumn), Timestamp(Buffer), Date(Buffer), - Interval(Buffer), Array(Box>), Nullable(Box>), Tuple { fields: Vec, len: usize }, @@ -128,7 +125,6 @@ pub enum ColumnBuilder { String(StringColumnBuilder), Timestamp(Vec), Date(Vec), - Interval(Vec), Array(Box>), Nullable(Box>), Tuple { @@ -217,7 +213,6 @@ impl Scalar { Scalar::String(s) => ScalarRef::String(s.as_slice()), Scalar::Timestamp(t) => ScalarRef::Timestamp(*t), Scalar::Date(d) => ScalarRef::Date(*d), - Scalar::Interval(i) => ScalarRef::Interval(*i), Scalar::Array(col) => ScalarRef::Array(col.clone()), Scalar::Tuple(fields) => ScalarRef::Tuple(fields.iter().map(Scalar::as_ref).collect()), Scalar::Variant(s) => ScalarRef::Variant(s.as_slice()), @@ -235,7 +230,6 @@ impl<'a> ScalarRef<'a> { ScalarRef::String(s) => Scalar::String(s.to_vec()), ScalarRef::Timestamp(t) => Scalar::Timestamp(*t), ScalarRef::Date(d) => Scalar::Date(*d), - ScalarRef::Interval(i) => Scalar::Interval(*i), ScalarRef::Array(col) => Scalar::Array(col.clone()), ScalarRef::Tuple(fields) => { Scalar::Tuple(fields.iter().map(ScalarRef::to_owned).collect()) @@ -266,7 +260,6 @@ impl<'a> ScalarRef<'a> { }), ScalarRef::Timestamp(t) => Domain::Timestamp(SimpleDomain { min: *t, max: *t }), ScalarRef::Date(d) => Domain::Date(SimpleDomain { min: *d, max: *d }), - ScalarRef::Interval(i) => Domain::Interval(SimpleDomain { min: *i, max: *i }), ScalarRef::Array(array) => Domain::Array(Some(Box::new(array.domain()))), ScalarRef::Tuple(fields) => { Domain::Tuple(fields.iter().map(|field| field.domain()).collect()) @@ -286,7 +279,6 @@ impl PartialOrd for Scalar { (Scalar::String(s1), Scalar::String(s2)) => s1.partial_cmp(s2), (Scalar::Timestamp(t1), Scalar::Timestamp(t2)) => t1.partial_cmp(t2), (Scalar::Date(d1), Scalar::Date(d2)) => d1.partial_cmp(d2), - (Scalar::Interval(i1), Scalar::Interval(i2)) => i1.partial_cmp(i2), (Scalar::Array(a1), Scalar::Array(a2)) => a1.partial_cmp(a2), (Scalar::Tuple(t1), Scalar::Tuple(t2)) => t1.partial_cmp(t2), (Scalar::Variant(v1), Scalar::Variant(v2)) => { @@ -310,7 +302,6 @@ impl PartialOrd for ScalarRef<'_> { (ScalarRef::String(s1), ScalarRef::String(s2)) => s1.partial_cmp(s2), (ScalarRef::Timestamp(t1), ScalarRef::Timestamp(t2)) => t1.partial_cmp(t2), (ScalarRef::Date(d1), ScalarRef::Date(d2)) => d1.partial_cmp(d2), - (ScalarRef::Interval(i1), ScalarRef::Interval(i2)) => i1.partial_cmp(i2), (ScalarRef::Array(a1), ScalarRef::Array(a2)) => a1.partial_cmp(a2), (ScalarRef::Tuple(t1), ScalarRef::Tuple(t2)) => t1.partial_cmp(t2), (ScalarRef::Variant(v1), ScalarRef::Variant(v2)) => match common_jsonb::compare(v1, v2) @@ -343,9 +334,6 @@ impl PartialOrd for Column { col1.iter().partial_cmp(col2.iter()) } (Column::Date(col1), Column::Date(col2)) => col1.iter().partial_cmp(col2.iter()), - (Column::Interval(col1), Column::Interval(col2)) => { - col1.iter().partial_cmp(col2.iter()) - } (Column::Array(col1), Column::Array(col2)) => col1.iter().partial_cmp(col2.iter()), (Column::Nullable(col1), Column::Nullable(col2)) => { col1.iter().partial_cmp(col2.iter()) @@ -376,7 +364,6 @@ impl Column { Column::String(col) => col.len(), Column::Timestamp(col) => col.len(), Column::Date(col) => col.len(), - Column::Interval(col) => col.len(), Column::Array(col) => col.len(), Column::Nullable(col) => col.len(), Column::Tuple { len, .. } => *len, @@ -393,7 +380,6 @@ impl Column { Column::String(col) => Some(ScalarRef::String(col.index(index)?)), Column::Timestamp(col) => Some(ScalarRef::Timestamp(col.get(index).cloned()?)), Column::Date(col) => Some(ScalarRef::Date(col.get(index).cloned()?)), - Column::Interval(col) => Some(ScalarRef::Interval(col.get(index).cloned()?)), Column::Array(col) => Some(ScalarRef::Array(col.index(index)?)), Column::Nullable(col) => Some(col.index(index)?.unwrap_or(ScalarRef::Null)), Column::Tuple { fields, .. } => Some(ScalarRef::Tuple( @@ -417,7 +403,6 @@ impl Column { Column::String(col) => ScalarRef::String(col.index_unchecked(index)), Column::Timestamp(col) => ScalarRef::Timestamp(*col.get_unchecked(index)), Column::Date(col) => ScalarRef::Date(*col.get_unchecked(index)), - Column::Interval(col) => ScalarRef::Interval(*col.get_unchecked(index)), Column::Array(col) => ScalarRef::Array(col.index_unchecked(index)), Column::Nullable(col) => col.index_unchecked(index).unwrap_or(ScalarRef::Null), Column::Tuple { fields, .. } => ScalarRef::Tuple( @@ -455,9 +440,6 @@ impl Column { Column::Date(col) => { Column::Date(col.clone().slice(range.start, range.end - range.start)) } - Column::Interval(col) => { - Column::Interval(col.clone().slice(range.start, range.end - range.start)) - } Column::Array(col) => Column::Array(Box::new(col.slice(range))), Column::Nullable(col) => Column::Nullable(Box::new(col.slice(range))), Column::Tuple { fields, .. } => Column::Tuple { @@ -513,13 +495,6 @@ impl Column { max: *max, }) } - Column::Interval(col) => { - let (min, max) = col.iter().minmax().into_option().unwrap(); - Domain::Interval(SimpleDomain { - min: *min, - max: *max, - }) - } Column::Array(col) => { let inner_domain = col.values.domain(); Domain::Array(Some(Box::new(inner_domain))) @@ -564,7 +539,6 @@ impl Column { Column::String { .. } => ArrowDataType::LargeBinary, Column::Timestamp(_) => ArrowDataType::Timestamp(TimeUnit::Microsecond, None), Column::Date(_) => ArrowDataType::Date32, - Column::Interval(_) => ArrowDataType::Date64, Column::Array(box ArrayColumn { values: Column::Nullable(box NullableColumn { column, .. }), .. @@ -710,13 +684,6 @@ impl Column { None, ), ), - Column::Interval(col) => Box::new( - common_arrow::arrow::array::PrimitiveArray::::from_data( - self.arrow_type(), - col.clone(), - None, - ), - ), Column::Array(col) => { Box::new(common_arrow::arrow::array::ListArray::::from_data( self.arrow_type(), @@ -934,14 +901,6 @@ impl Column { .values() .clone(), ), - ArrowDataType::Date64 => Column::Interval( - arrow_col - .as_any() - .downcast_ref::() - .expect("fail to read from arrow: array should be `Int64Array`") - .values() - .clone(), - ), ArrowDataType::Extension(name, _, None) if name == "Variant" => { let arrow_col = arrow_col .as_any() @@ -1026,7 +985,6 @@ impl Column { Column::String(col) => col.data.len() + col.offsets.len() * 8, Column::Timestamp(col) => col.len() * 8, Column::Date(col) => col.len() * 4, - Column::Interval(col) => col.len() * 8, Column::Array(col) => col.values.memory_size() + col.offsets.len() * 8, Column::Nullable(c) => c.column.memory_size() + c.validity.as_slice().0.len(), Column::Tuple { fields, .. } => fields.iter().map(|f| f.memory_size()).sum(), @@ -1076,7 +1034,6 @@ impl ColumnBuilder { Column::String(col) => ColumnBuilder::String(StringColumnBuilder::from_column(col)), Column::Timestamp(col) => ColumnBuilder::Timestamp(buffer_into_mut(col)), Column::Date(col) => ColumnBuilder::Date(buffer_into_mut(col)), - Column::Interval(col) => ColumnBuilder::Interval(buffer_into_mut(col)), Column::Array(box col) => { ColumnBuilder::Array(Box::new(ArrayColumnBuilder::from_column(col))) } @@ -1116,7 +1073,6 @@ impl ColumnBuilder { ScalarRef::String(s) => ColumnBuilder::String(StringColumnBuilder::repeat(s, n)), ScalarRef::Timestamp(d) => ColumnBuilder::Timestamp(vec![*d; n]), ScalarRef::Date(d) => ColumnBuilder::Date(vec![*d; n]), - ScalarRef::Interval(i) => ColumnBuilder::Interval(vec![*i; n]), ScalarRef::Array(col) => { ColumnBuilder::Array(Box::new(ArrayColumnBuilder::repeat(col, n))) } @@ -1147,7 +1103,6 @@ impl ColumnBuilder { ColumnBuilder::String(builder) => builder.len(), ColumnBuilder::Timestamp(builder) => builder.len(), ColumnBuilder::Date(builder) => builder.len(), - ColumnBuilder::Interval(builder) => builder.len(), ColumnBuilder::Array(builder) => builder.len(), ColumnBuilder::Nullable(builder) => builder.len(), ColumnBuilder::Tuple { len, .. } => *len, @@ -1172,9 +1127,6 @@ impl ColumnBuilder { DataType::Date => { ColumnBuilder::Number(NumberColumnBuilder::Int32(Vec::with_capacity(capacity))) } - DataType::Interval => { - ColumnBuilder::Number(NumberColumnBuilder::Int64(Vec::with_capacity(capacity))) - } DataType::Nullable(ty) => ColumnBuilder::Nullable(Box::new(NullableColumnBuilder { builder: Self::with_capacity(ty, capacity), validity: MutableBitmap::with_capacity(capacity), @@ -1224,7 +1176,6 @@ impl ColumnBuilder { builder.push(value); } (ColumnBuilder::Date(builder), ScalarRef::Date(value)) => builder.push(value), - (ColumnBuilder::Interval(builder), ScalarRef::Interval(value)) => builder.push(value), (ColumnBuilder::Array(builder), ScalarRef::Array(value)) => { builder.push(value); } @@ -1258,7 +1209,6 @@ impl ColumnBuilder { ColumnBuilder::String(builder) => builder.commit_row(), ColumnBuilder::Timestamp(builder) => builder.push(0), ColumnBuilder::Date(builder) => builder.push(0), - ColumnBuilder::Interval(builder) => builder.push(0), ColumnBuilder::Array(builder) => builder.push_default(), ColumnBuilder::Nullable(builder) => builder.push_null(), ColumnBuilder::Tuple { fields, len } => { @@ -1297,9 +1247,6 @@ impl ColumnBuilder { (ColumnBuilder::Date(builder), ColumnBuilder::Date(other_builder)) => { builder.extend_from_slice(other_builder); } - (ColumnBuilder::Interval(builder), ColumnBuilder::Interval(other_builder)) => { - builder.extend_from_slice(other_builder); - } (ColumnBuilder::Array(builder), ColumnBuilder::Array(other_builder)) => { builder.append(other_builder); } @@ -1332,7 +1279,6 @@ impl ColumnBuilder { ColumnBuilder::String(builder) => Column::String(builder.build()), ColumnBuilder::Timestamp(builder) => Column::Timestamp(builder.into()), ColumnBuilder::Date(builder) => Column::Date(builder.into()), - ColumnBuilder::Interval(builder) => Column::Interval(builder.into()), ColumnBuilder::Array(builder) => Column::Array(Box::new(builder.build())), ColumnBuilder::Nullable(builder) => Column::Nullable(Box::new(builder.build())), ColumnBuilder::Tuple { fields, len } => Column::Tuple { @@ -1353,7 +1299,6 @@ impl ColumnBuilder { ColumnBuilder::String(builder) => Scalar::String(builder.build_scalar()), ColumnBuilder::Timestamp(builder) => Scalar::Timestamp(builder[0]), ColumnBuilder::Date(builder) => Scalar::Date(builder[0]), - ColumnBuilder::Interval(builder) => Scalar::Interval(builder[0]), ColumnBuilder::Array(builder) => Scalar::Array(builder.build_scalar()), ColumnBuilder::Nullable(builder) => builder.build_scalar().unwrap_or(Scalar::Null), ColumnBuilder::Tuple { fields, .. } => Scalar::Tuple( diff --git a/src/query/functions-v2/src/aggregates/aggregate_scalar_state.rs b/src/query/functions-v2/src/aggregates/aggregate_scalar_state.rs index 77e13b57c26a..b63626fddaf7 100644 --- a/src/query/functions-v2/src/aggregates/aggregate_scalar_state.rs +++ b/src/query/functions-v2/src/aggregates/aggregate_scalar_state.rs @@ -40,7 +40,6 @@ macro_rules! with_simple_no_number_mapped_type { Null => NullType, EmptyArray => EmptyArrayType, Date => DateType, - Interval => IntervalType, ], $($tail)* } From 32a251cd91abf2f09b836163d1fcd31d60c751f9 Mon Sep 17 00:00:00 2001 From: RinChanNOWWW Date: Fri, 14 Oct 2022 19:43:05 +0800 Subject: [PATCH 5/8] Add interval arithmetic for date and timestamp. --- src/query/expression/tests/it/convert.rs | 3 +- .../functions-v2/tests/it/scalars/datetime.rs | 265 +++++++ .../functions-v2/tests/it/scalars/parser.rs | 132 +++- .../tests/it/scalars/testdata/datetime.txt | 720 ++++++++++++++++++ 4 files changed, 1078 insertions(+), 42 deletions(-) diff --git a/src/query/expression/tests/it/convert.rs b/src/query/expression/tests/it/convert.rs index 0dc6fd588644..ab604ba60e67 100644 --- a/src/query/expression/tests/it/convert.rs +++ b/src/query/expression/tests/it/convert.rs @@ -25,7 +25,7 @@ fn random_type() -> DataTypeImpl { let mut rng = rand::thread_rng(); loop { - let datatype = match rng.gen_range(0..22) { + let datatype = match rng.gen_range(0..21) { 0 => DataTypeImpl::Null(NullType {}), 1 => NullableType::new_impl(u64::to_data_type()), 2 => DataTypeImpl::Boolean(BooleanType {}), @@ -50,7 +50,6 @@ fn random_type() -> DataTypeImpl { 17 => ArrayType::new_impl(f32::to_data_type()), 18 => DataTypeImpl::VariantArray(VariantArrayType {}), 19 => DataTypeImpl::VariantObject(VariantObjectType {}), - 20 => DataTypeImpl::Interval(IntervalType::new(IntervalKind::Day)), _ => DataTypeImpl::Variant(VariantType {}), }; if can_convert(&datatype) { diff --git a/src/query/functions-v2/tests/it/scalars/datetime.rs b/src/query/functions-v2/tests/it/scalars/datetime.rs index 0ce73b99f4b1..4702af6e27c5 100644 --- a/src/query/functions-v2/tests/it/scalars/datetime.rs +++ b/src/query/functions-v2/tests/it/scalars/datetime.rs @@ -38,6 +38,9 @@ fn test_datetime() { // date_{add | sub}({year | quarter | month | week | day | hour | minute | second}, date, number) test_date_date_add_sub(file); test_timestamp_date_add_sub(file); + // date {+ | -} interval number {year | quarter | month | week | day | hour | minute | second} + test_date_arith(file); + test_timestamp_arith(file); } fn test_to_timestamp(file: &mut impl Write) { @@ -630,3 +633,265 @@ fn test_timestamp_date_add_sub(file: &mut impl Write) { ), ]); } + +fn test_date_arith(file: &mut impl Write) { + run_ast(file, "to_date(0) + interval 10000 year", &[]); // failed + run_ast(file, "to_date(0) + interval 100 year", &[]); + run_ast(file, "to_date(0) + interval 100 quarter", &[]); + run_ast(file, "to_date(0) + interval 100 month", &[]); + run_ast(file, "to_date(0) + interval 100 day", &[]); + run_ast(file, "to_date(0) - interval 100 year", &[]); + run_ast(file, "to_date(0) - interval 100 quarter", &[]); + run_ast(file, "to_date(0) - interval 100 month", &[]); + run_ast(file, "to_date(0) - interval 100 day", &[]); + run_ast(file, "a + interval b year", &[ + ("a", DataType::Date, from_date_data(vec![-100, 0, 100])), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); + run_ast(file, "a + interval b quarter", &[ + ("a", DataType::Date, from_date_data(vec![-100, 0, 100])), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); + run_ast(file, "a + interval b month", &[ + ("a", DataType::Date, from_date_data(vec![-100, 0, 100])), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); + run_ast(file, "a + interval b day", &[ + ("a", DataType::Date, from_date_data(vec![-100, 0, 100])), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); + run_ast(file, "a - interval b year", &[ + ("a", DataType::Date, from_date_data(vec![-100, 0, 100])), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); + run_ast(file, "a - interval b quarter", &[ + ("a", DataType::Date, from_date_data(vec![-100, 0, 100])), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); + run_ast(file, "a - interval b month", &[ + ("a", DataType::Date, from_date_data(vec![-100, 0, 100])), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); + run_ast(file, "a - interval b day", &[ + ("a", DataType::Date, from_date_data(vec![-100, 0, 100])), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); +} + +fn test_timestamp_arith(file: &mut impl Write) { + run_ast(file, "to_timestamp(0) + interval 10000 year", &[]); // failed + run_ast(file, "to_timestamp(0) + interval 100 year", &[]); + run_ast(file, "to_timestamp(0) + interval 100 quarter", &[]); + run_ast(file, "to_timestamp(0) + interval 100 month", &[]); + run_ast(file, "to_timestamp(0) + interval 100 day", &[]); + run_ast(file, "to_timestamp(0) + interval 100 hour", &[]); + run_ast(file, "to_timestamp(0) + interval 100 minute", &[]); + run_ast(file, "to_timestamp(0) + interval 100 second", &[]); + run_ast(file, "to_timestamp(0) - interval 100 year", &[]); + run_ast(file, "to_timestamp(0) - interval 100 quarter", &[]); + run_ast(file, "to_timestamp(0) - interval 100 month", &[]); + run_ast(file, "to_timestamp(0) - interval 100 day", &[]); + run_ast(file, "to_timestamp(0) - interval 100 hour", &[]); + run_ast(file, "to_timestamp(0) - interval 100 minute", &[]); + run_ast(file, "to_timestamp(0) - interval 100 second", &[]); + run_ast(file, "a + interval b year", &[ + ( + "a", + DataType::Timestamp, + from_timestamp_data(vec![-100, 0, 100]), + ), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); + run_ast(file, "a + interval b quarter", &[ + ( + "a", + DataType::Timestamp, + from_timestamp_data(vec![-100, 0, 100]), + ), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); + run_ast(file, "a + interval b month", &[ + ( + "a", + DataType::Timestamp, + from_timestamp_data(vec![-100, 0, 100]), + ), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); + run_ast(file, "a + interval b day", &[ + ( + "a", + DataType::Timestamp, + from_timestamp_data(vec![-100, 0, 100]), + ), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); + run_ast(file, "a + interval b hour", &[ + ( + "a", + DataType::Timestamp, + from_timestamp_data(vec![-100, 0, 100]), + ), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); + run_ast(file, "a + interval b minute", &[ + ( + "a", + DataType::Timestamp, + from_timestamp_data(vec![-100, 0, 100]), + ), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); + run_ast(file, "a + interval b second", &[ + ( + "a", + DataType::Timestamp, + from_timestamp_data(vec![-100, 0, 100]), + ), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); + run_ast(file, "a - interval b year", &[ + ( + "a", + DataType::Timestamp, + from_timestamp_data(vec![-100, 0, 100]), + ), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); + run_ast(file, "a - interval b quarter", &[ + ( + "a", + DataType::Timestamp, + from_timestamp_data(vec![-100, 0, 100]), + ), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); + run_ast(file, "a - interval b month", &[ + ( + "a", + DataType::Timestamp, + from_timestamp_data(vec![-100, 0, 100]), + ), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); + run_ast(file, "a - interval b day", &[ + ( + "a", + DataType::Timestamp, + from_timestamp_data(vec![-100, 0, 100]), + ), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); + run_ast(file, "a - interval b hour", &[ + ( + "a", + DataType::Timestamp, + from_timestamp_data(vec![-100, 0, 100]), + ), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); + run_ast(file, "a - interval b minute", &[ + ( + "a", + DataType::Timestamp, + from_timestamp_data(vec![-100, 0, 100]), + ), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); + run_ast(file, "a - interval b second", &[ + ( + "a", + DataType::Timestamp, + from_timestamp_data(vec![-100, 0, 100]), + ), + ( + "b", + DataType::Number(NumberDataType::Int32), + Column::from_data(vec![1, 2, 3]), + ), + ]); +} diff --git a/src/query/functions-v2/tests/it/scalars/parser.rs b/src/query/functions-v2/tests/it/scalars/parser.rs index 87569eef13b9..19ed2027aa1a 100644 --- a/src/query/functions-v2/tests/it/scalars/parser.rs +++ b/src/query/functions-v2/tests/it/scalars/parser.rs @@ -47,6 +47,44 @@ macro_rules! with_interval_mapped_name { } } +macro_rules! transform_interval_add_sub { + ($span: expr, $columns: expr, $name: expr, $unit: expr, $date: expr, $interval: expr) => { + if $name == "plus" { + with_interval_mapped_name!(|INTERVAL| match $unit { + IntervalKind::INTERVAL => RawExpr::FunctionCall { + span: transform_span($span), + name: concat!("add_", INTERVAL).to_string(), + params: vec![], + args: vec![ + transform_expr(*$date, $columns), + transform_expr(*$interval, $columns), + ], + }, + kind => { + unimplemented!("{kind:?} is not supported for interval") + } + }) + } else if $name == "minus" { + with_interval_mapped_name!(|INTERVAL| match $unit { + IntervalKind::INTERVAL => RawExpr::FunctionCall { + span: transform_span($span), + name: concat!("substract_", INTERVAL).to_string(), + params: vec![], + args: vec![ + transform_expr(*$date, $columns), + transform_expr(*$interval, $columns), + ], + }, + kind => { + unimplemented!("{kind:?} is not supported for interval") + } + }) + } else { + unimplemented!("operator {} is not supported for interval", $name) + } + }; +} + pub fn transform_expr(ast: common_ast::ast::Expr, columns: &[(&str, DataType)]) -> RawExpr { match ast { common_ast::ast::Expr::Literal { span, lit } => RawExpr::Literal { @@ -151,48 +189,62 @@ pub fn transform_expr(ast: common_ast::ast::Expr, columns: &[(&str, DataType)]) right, } => { let name = transform_binary_op(op); - if name != "notlike" && name != "notregexp" && name != "notrlike" { - RawExpr::FunctionCall { - span: transform_span(span), - name, - params: vec![], - args: vec![ - transform_expr(*left, columns), - transform_expr(*right, columns), - ], - } - } else if name == "notlike" { - let result = RawExpr::FunctionCall { - span: transform_span(span), - name: "like".to_string(), - params: vec![], - args: vec![ - transform_expr(*left, columns), - transform_expr(*right, columns), - ], - }; - RawExpr::FunctionCall { - span: transform_span(span), - name: "not".to_string(), - params: vec![], - args: vec![result], + match name.as_str() { + "notlike" => { + let result = RawExpr::FunctionCall { + span: transform_span(span), + name: "like".to_string(), + params: vec![], + args: vec![ + transform_expr(*left, columns), + transform_expr(*right, columns), + ], + }; + RawExpr::FunctionCall { + span: transform_span(span), + name: "not".to_string(), + params: vec![], + args: vec![result], + } } - } else { - let result = RawExpr::FunctionCall { - span: transform_span(span), - name: "regexp".to_string(), - params: vec![], - args: vec![ - transform_expr(*left, columns), - transform_expr(*right, columns), - ], - }; - RawExpr::FunctionCall { - span: transform_span(span), - name: "not".to_string(), - params: vec![], - args: vec![result], + "notregexp" | "notrlike" => { + let result = RawExpr::FunctionCall { + span: transform_span(span), + name: "regexp".to_string(), + params: vec![], + args: vec![ + transform_expr(*left, columns), + transform_expr(*right, columns), + ], + }; + RawExpr::FunctionCall { + span: transform_span(span), + name: "not".to_string(), + params: vec![], + args: vec![result], + } } + _ => match (*left.clone(), *right.clone()) { + (common_ast::ast::Expr::Interval { expr, unit, .. }, _) => { + if name == "minus" { + unimplemented!("interval cannot be the minuend") + } else { + transform_interval_add_sub!(span, columns, name, unit, right, expr) + } + } + (_, common_ast::ast::Expr::Interval { expr, unit, .. }) => { + transform_interval_add_sub!(span, columns, name, unit, left, expr) + } + (_, _) => RawExpr::FunctionCall { + span: transform_span(span), + name, + params: vec![], + args: vec![ + transform_expr(*left, columns), + transform_expr(*right, columns), + ], + }, + }, } } common_ast::ast::Expr::Position { diff --git a/src/query/functions-v2/tests/it/scalars/testdata/datetime.txt b/src/query/functions-v2/tests/it/scalars/testdata/datetime.txt index 9db5a841cf4c..2f6f0f63c7f2 100644 --- a/src/query/functions-v2/tests/it/scalars/testdata/datetime.txt +++ b/src/query/functions-v2/tests/it/scalars/testdata/datetime.txt @@ -1751,3 +1751,723 @@ evaluation (internal): +--------+--------------------------------+ +error: + --> SQL:1:12 + | +1 | to_date(0) + interval 10000 year + | ^ date `+11970-01-01` is out of range + + + +ast : to_date(0) + interval 100 year +raw expr : add_years(to_date(0_u8), 100_u8) +checked expr : add_years(to_date(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : 36525 +output type : Date +output domain : Unknown +output : 2070-01-01 + + +ast : to_date(0) + interval 100 quarter +raw expr : add_quarters(to_date(0_u8), 100_u8) +checked expr : add_quarters(to_date(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : 9131 +output type : Date +output domain : Unknown +output : 1995-01-01 + + +ast : to_date(0) + interval 100 month +raw expr : add_months(to_date(0_u8), 100_u8) +checked expr : add_months(to_date(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : 3042 +output type : Date +output domain : Unknown +output : 1978-05-01 + + +ast : to_date(0) + interval 100 day +raw expr : add_days(to_date(0_u8), 100_u8) +checked expr : add_days(to_date(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : 100 +output type : Date +output domain : Unknown +output : 1970-04-11 + + +ast : to_date(0) - interval 100 year +raw expr : substract_years(to_date(0_u8), 100_u8) +checked expr : substract_years(to_date(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : -36524 +output type : Date +output domain : Unknown +output : 1870-01-01 + + +ast : to_date(0) - interval 100 quarter +raw expr : substract_quarters(to_date(0_u8), 100_u8) +checked expr : substract_quarters(to_date(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : -9131 +output type : Date +output domain : Unknown +output : 1945-01-01 + + +ast : to_date(0) - interval 100 month +raw expr : substract_months(to_date(0_u8), 100_u8) +checked expr : substract_months(to_date(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : -3044 +output type : Date +output domain : Unknown +output : 1961-09-01 + + +ast : to_date(0) - interval 100 day +raw expr : substract_days(to_date(0_u8), 100_u8) +checked expr : substract_days(to_date(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : -100 +output type : Date +output domain : Unknown +output : 1969-09-23 + + +ast : a + interval b year +raw expr : add_years(ColumnRef(0)::Date, ColumnRef(1)::Int32) +checked expr : add_years(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+--------------+---------+------------+ +| | a | b | Output | ++--------+--------------+---------+------------+ +| Type | Date | Int32 | Date | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-09-23 | 1 | 1970-09-23 | +| Row 1 | 1970-01-01 | 2 | 1972-01-01 | +| Row 2 | 1970-04-11 | 3 | 1973-04-11 | ++--------+--------------+---------+------------+ +evaluation (internal): ++--------+------------------+ +| Column | Data | ++--------+------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [265, 730, 1196] | ++--------+------------------+ + + +ast : a + interval b quarter +raw expr : add_quarters(ColumnRef(0)::Date, ColumnRef(1)::Int32) +checked expr : add_quarters(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+--------------+---------+------------+ +| | a | b | Output | ++--------+--------------+---------+------------+ +| Type | Date | Int32 | Date | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-09-23 | 1 | 1969-12-23 | +| Row 1 | 1970-01-01 | 2 | 1970-07-01 | +| Row 2 | 1970-04-11 | 3 | 1971-01-11 | ++--------+--------------+---------+------------+ +evaluation (internal): ++--------+------------------+ +| Column | Data | ++--------+------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [-9, 181, 375] | ++--------+------------------+ + + +ast : a + interval b month +raw expr : add_months(ColumnRef(0)::Date, ColumnRef(1)::Int32) +checked expr : add_months(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+--------------+---------+------------+ +| | a | b | Output | ++--------+--------------+---------+------------+ +| Type | Date | Int32 | Date | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-09-23 | 1 | 1969-10-23 | +| Row 1 | 1970-01-01 | 2 | 1970-03-01 | +| Row 2 | 1970-04-11 | 3 | 1970-07-11 | ++--------+--------------+---------+------------+ +evaluation (internal): ++--------+------------------+ +| Column | Data | ++--------+------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [-70, 59, 191] | ++--------+------------------+ + + +ast : a + interval b day +raw expr : add_days(ColumnRef(0)::Date, ColumnRef(1)::Int32) +checked expr : add_days(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+--------------+---------+------------+ +| | a | b | Output | ++--------+--------------+---------+------------+ +| Type | Date | Int32 | Date | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-09-23 | 1 | 1969-09-24 | +| Row 1 | 1970-01-01 | 2 | 1970-01-03 | +| Row 2 | 1970-04-11 | 3 | 1970-04-14 | ++--------+--------------+---------+------------+ +evaluation (internal): ++--------+------------------+ +| Column | Data | ++--------+------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [-99, 2, 103] | ++--------+------------------+ + + +ast : a - interval b year +raw expr : substract_years(ColumnRef(0)::Date, ColumnRef(1)::Int32) +checked expr : substract_years(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+--------------+---------+------------+ +| | a | b | Output | ++--------+--------------+---------+------------+ +| Type | Date | Int32 | Date | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-09-23 | 1 | 1968-09-23 | +| Row 1 | 1970-01-01 | 2 | 1968-01-01 | +| Row 2 | 1970-04-11 | 3 | 1967-04-11 | ++--------+--------------+---------+------------+ +evaluation (internal): ++--------+--------------------+ +| Column | Data | ++--------+--------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [-465, -731, -996] | ++--------+--------------------+ + + +ast : a - interval b quarter +raw expr : substract_quarters(ColumnRef(0)::Date, ColumnRef(1)::Int32) +checked expr : substract_quarters(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+--------------+---------+------------+ +| | a | b | Output | ++--------+--------------+---------+------------+ +| Type | Date | Int32 | Date | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-09-23 | 1 | 1969-06-23 | +| Row 1 | 1970-01-01 | 2 | 1969-07-01 | +| Row 2 | 1970-04-11 | 3 | 1969-07-11 | ++--------+--------------+---------+------------+ +evaluation (internal): ++--------+--------------------+ +| Column | Data | ++--------+--------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [-192, -184, -174] | ++--------+--------------------+ + + +ast : a - interval b month +raw expr : substract_months(ColumnRef(0)::Date, ColumnRef(1)::Int32) +checked expr : substract_months(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+--------------+---------+------------+ +| | a | b | Output | ++--------+--------------+---------+------------+ +| Type | Date | Int32 | Date | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-09-23 | 1 | 1969-08-23 | +| Row 1 | 1970-01-01 | 2 | 1969-11-01 | +| Row 2 | 1970-04-11 | 3 | 1970-01-11 | ++--------+--------------+---------+------------+ +evaluation (internal): ++--------+------------------+ +| Column | Data | ++--------+------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [-131, -61, 10] | ++--------+------------------+ + + +ast : a - interval b day +raw expr : substract_days(ColumnRef(0)::Date, ColumnRef(1)::Int32) +checked expr : substract_days(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+--------------+---------+------------+ +| | a | b | Output | ++--------+--------------+---------+------------+ +| Type | Date | Int32 | Date | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-09-23 | 1 | 1969-09-22 | +| Row 1 | 1970-01-01 | 2 | 1969-12-30 | +| Row 2 | 1970-04-11 | 3 | 1970-04-08 | ++--------+--------------+---------+------------+ +evaluation (internal): ++--------+------------------+ +| Column | Data | ++--------+------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [-101, -2, 97] | ++--------+------------------+ + + +error: + --> SQL:1:17 + | +1 | to_timestamp(0) + interval 10000 year + | ^ timestamp `315569520000000000` is out of range + + + +ast : to_timestamp(0) + interval 100 year +raw expr : add_years(to_timestamp(0_u8), 100_u8) +checked expr : add_years(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : 3155760000000000 +output type : Timestamp +output domain : Unknown +output : 2070-01-01 00:00:00.000000 + + +ast : to_timestamp(0) + interval 100 quarter +raw expr : add_quarters(to_timestamp(0_u8), 100_u8) +checked expr : add_quarters(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : 788918400000000 +output type : Timestamp +output domain : Unknown +output : 1995-01-01 00:00:00.000000 + + +ast : to_timestamp(0) + interval 100 month +raw expr : add_months(to_timestamp(0_u8), 100_u8) +checked expr : add_months(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : 262828800000000 +output type : Timestamp +output domain : Unknown +output : 1978-05-01 00:00:00.000000 + + +ast : to_timestamp(0) + interval 100 day +raw expr : add_days(to_timestamp(0_u8), 100_u8) +checked expr : add_days(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : 8640000000000 +output type : Timestamp +output domain : Unknown +output : 1970-04-11 00:00:00.000000 + + +ast : to_timestamp(0) + interval 100 hour +raw expr : add_hours(to_timestamp(0_u8), 100_u8) +checked expr : add_hours(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : 360000000000 +output type : Timestamp +output domain : Unknown +output : 1970-01-05 04:00:00.000000 + + +ast : to_timestamp(0) + interval 100 minute +raw expr : add_minutes(to_timestamp(0_u8), 100_u8) +checked expr : add_minutes(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : 6000000000 +output type : Timestamp +output domain : Unknown +output : 1970-01-01 01:40:00.000000 + + +ast : to_timestamp(0) + interval 100 second +raw expr : add_seconds(to_timestamp(0_u8), 100_u8) +checked expr : add_seconds(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : 100000000 +output type : Timestamp +output domain : Unknown +output : 1970-01-01 00:01:40.000000 + + +ast : to_timestamp(0) - interval 100 year +raw expr : substract_years(to_timestamp(0_u8), 100_u8) +checked expr : substract_years(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : -3155673600000000 +output type : Timestamp +output domain : Unknown +output : 1870-01-01 00:00:00.000000 + + +ast : to_timestamp(0) - interval 100 quarter +raw expr : substract_quarters(to_timestamp(0_u8), 100_u8) +checked expr : substract_quarters(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : -788918400000000 +output type : Timestamp +output domain : Unknown +output : 1945-01-01 00:00:00.000000 + + +ast : to_timestamp(0) - interval 100 month +raw expr : substract_months(to_timestamp(0_u8), 100_u8) +checked expr : substract_months(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : -263001600000000 +output type : Timestamp +output domain : Unknown +output : 1961-09-01 00:00:00.000000 + + +ast : to_timestamp(0) - interval 100 day +raw expr : substract_days(to_timestamp(0_u8), 100_u8) +checked expr : substract_days(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : -8640000000000 +output type : Timestamp +output domain : Unknown +output : 1969-09-23 00:00:00.000000 + + +ast : to_timestamp(0) - interval 100 hour +raw expr : substract_hours(to_timestamp(0_u8), 100_u8) +checked expr : substract_hours(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : -360000000000 +output type : Timestamp +output domain : Unknown +output : 1969-12-27 20:00:00.000000 + + +ast : to_timestamp(0) - interval 100 minute +raw expr : substract_minutes(to_timestamp(0_u8), 100_u8) +checked expr : substract_minutes(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : -6000000000 +output type : Timestamp +output domain : Unknown +output : 1969-12-31 22:20:00.000000 + + +ast : to_timestamp(0) - interval 100 second +raw expr : substract_seconds(to_timestamp(0_u8), 100_u8) +checked expr : substract_seconds(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +optimized expr : -100000000 +output type : Timestamp +output domain : Unknown +output : 1969-12-31 23:58:20.000000 + + +ast : a + interval b year +raw expr : add_years(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) +checked expr : add_years(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+----------------------------+---------+----------------------------+ +| | a | b | Output | ++--------+----------------------------+---------+----------------------------+ +| Type | Timestamp | Int32 | Timestamp | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-12-31 23:59:59.999900 | 1 | 1970-12-31 23:59:59.999900 | +| Row 1 | 1970-01-01 00:00:00.000000 | 2 | 1972-01-01 00:00:00.000000 | +| Row 2 | 1970-01-01 00:00:00.000100 | 3 | 1973-01-01 00:00:00.000100 | ++--------+----------------------------+---------+----------------------------+ +evaluation (internal): ++--------+--------------------------------------------------+ +| Column | Data | ++--------+--------------------------------------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [31535999999900, 63072000000000, 94694400000100] | ++--------+--------------------------------------------------+ + + +ast : a + interval b quarter +raw expr : add_quarters(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) +checked expr : add_quarters(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+----------------------------+---------+----------------------------+ +| | a | b | Output | ++--------+----------------------------+---------+----------------------------+ +| Type | Timestamp | Int32 | Timestamp | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-12-31 23:59:59.999900 | 1 | 1970-03-31 23:59:59.999900 | +| Row 1 | 1970-01-01 00:00:00.000000 | 2 | 1970-07-01 00:00:00.000000 | +| Row 2 | 1970-01-01 00:00:00.000100 | 3 | 1970-10-01 00:00:00.000100 | ++--------+----------------------------+---------+----------------------------+ +evaluation (internal): ++--------+-------------------------------------------------+ +| Column | Data | ++--------+-------------------------------------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [7775999999900, 15638400000000, 23587200000100] | ++--------+-------------------------------------------------+ + + +ast : a + interval b month +raw expr : add_months(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) +checked expr : add_months(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+----------------------------+---------+----------------------------+ +| | a | b | Output | ++--------+----------------------------+---------+----------------------------+ +| Type | Timestamp | Int32 | Timestamp | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-12-31 23:59:59.999900 | 1 | 1970-01-31 23:59:59.999900 | +| Row 1 | 1970-01-01 00:00:00.000000 | 2 | 1970-03-01 00:00:00.000000 | +| Row 2 | 1970-01-01 00:00:00.000100 | 3 | 1970-04-01 00:00:00.000100 | ++--------+----------------------------+---------+----------------------------+ +evaluation (internal): ++--------+-----------------------------------------------+ +| Column | Data | ++--------+-----------------------------------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [2678399999900, 5097600000000, 7776000000100] | ++--------+-----------------------------------------------+ + + +ast : a + interval b day +raw expr : add_days(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) +checked expr : add_days(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+----------------------------+---------+----------------------------+ +| | a | b | Output | ++--------+----------------------------+---------+----------------------------+ +| Type | Timestamp | Int32 | Timestamp | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-12-31 23:59:59.999900 | 1 | 1970-01-01 23:59:59.999900 | +| Row 1 | 1970-01-01 00:00:00.000000 | 2 | 1970-01-03 00:00:00.000000 | +| Row 2 | 1970-01-01 00:00:00.000100 | 3 | 1970-01-04 00:00:00.000100 | ++--------+----------------------------+---------+----------------------------+ +evaluation (internal): ++--------+-------------------------------------------+ +| Column | Data | ++--------+-------------------------------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [86399999900, 172800000000, 259200000100] | ++--------+-------------------------------------------+ + + +ast : a + interval b hour +raw expr : add_hours(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) +checked expr : add_hours(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+----------------------------+---------+----------------------------+ +| | a | b | Output | ++--------+----------------------------+---------+----------------------------+ +| Type | Timestamp | Int32 | Timestamp | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-12-31 23:59:59.999900 | 1 | 1970-01-01 00:59:59.999900 | +| Row 1 | 1970-01-01 00:00:00.000000 | 2 | 1970-01-01 02:00:00.000000 | +| Row 2 | 1970-01-01 00:00:00.000100 | 3 | 1970-01-01 03:00:00.000100 | ++--------+----------------------------+---------+----------------------------+ +evaluation (internal): ++--------+---------------------------------------+ +| Column | Data | ++--------+---------------------------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [3599999900, 7200000000, 10800000100] | ++--------+---------------------------------------+ + + +ast : a + interval b minute +raw expr : add_minutes(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) +checked expr : add_minutes(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+----------------------------+---------+----------------------------+ +| | a | b | Output | ++--------+----------------------------+---------+----------------------------+ +| Type | Timestamp | Int32 | Timestamp | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-12-31 23:59:59.999900 | 1 | 1970-01-01 00:00:59.999900 | +| Row 1 | 1970-01-01 00:00:00.000000 | 2 | 1970-01-01 00:02:00.000000 | +| Row 2 | 1970-01-01 00:00:00.000100 | 3 | 1970-01-01 00:03:00.000100 | ++--------+----------------------------+---------+----------------------------+ +evaluation (internal): ++--------+----------------------------------+ +| Column | Data | ++--------+----------------------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [59999900, 120000000, 180000100] | ++--------+----------------------------------+ + + +ast : a + interval b second +raw expr : add_seconds(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) +checked expr : add_seconds(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+----------------------------+---------+----------------------------+ +| | a | b | Output | ++--------+----------------------------+---------+----------------------------+ +| Type | Timestamp | Int32 | Timestamp | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-12-31 23:59:59.999900 | 1 | 1970-01-01 00:00:00.999900 | +| Row 1 | 1970-01-01 00:00:00.000000 | 2 | 1970-01-01 00:00:02.000000 | +| Row 2 | 1970-01-01 00:00:00.000100 | 3 | 1970-01-01 00:00:03.000100 | ++--------+----------------------------+---------+----------------------------+ +evaluation (internal): ++--------+----------------------------+ +| Column | Data | ++--------+----------------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [999900, 2000000, 3000100] | ++--------+----------------------------+ + + +ast : a - interval b year +raw expr : substract_years(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) +checked expr : substract_years(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+----------------------------+---------+----------------------------+ +| | a | b | Output | ++--------+----------------------------+---------+----------------------------+ +| Type | Timestamp | Int32 | Timestamp | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-12-31 23:59:59.999900 | 1 | 1968-12-31 23:59:59.999900 | +| Row 1 | 1970-01-01 00:00:00.000000 | 2 | 1968-01-01 00:00:00.000000 | +| Row 2 | 1970-01-01 00:00:00.000100 | 3 | 1967-01-01 00:00:00.000100 | ++--------+----------------------------+---------+----------------------------+ +evaluation (internal): ++--------+-----------------------------------------------------+ +| Column | Data | ++--------+-----------------------------------------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [-31536000000100, -63158400000000, -94694399999900] | ++--------+-----------------------------------------------------+ + + +ast : a - interval b quarter +raw expr : substract_quarters(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) +checked expr : substract_quarters(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+----------------------------+---------+----------------------------+ +| | a | b | Output | ++--------+----------------------------+---------+----------------------------+ +| Type | Timestamp | Int32 | Timestamp | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-12-31 23:59:59.999900 | 1 | 1969-09-30 23:59:59.999900 | +| Row 1 | 1970-01-01 00:00:00.000000 | 2 | 1969-07-01 00:00:00.000000 | +| Row 2 | 1970-01-01 00:00:00.000100 | 3 | 1969-04-01 00:00:00.000100 | ++--------+----------------------------+---------+----------------------------+ +evaluation (internal): ++--------+----------------------------------------------------+ +| Column | Data | ++--------+----------------------------------------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [-7948800000100, -15897600000000, -23759999999900] | ++--------+----------------------------------------------------+ + + +ast : a - interval b month +raw expr : substract_months(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) +checked expr : substract_months(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+----------------------------+---------+----------------------------+ +| | a | b | Output | ++--------+----------------------------+---------+----------------------------+ +| Type | Timestamp | Int32 | Timestamp | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-12-31 23:59:59.999900 | 1 | 1969-11-30 23:59:59.999900 | +| Row 1 | 1970-01-01 00:00:00.000000 | 2 | 1969-11-01 00:00:00.000000 | +| Row 2 | 1970-01-01 00:00:00.000100 | 3 | 1969-10-01 00:00:00.000100 | ++--------+----------------------------+---------+----------------------------+ +evaluation (internal): ++--------+--------------------------------------------------+ +| Column | Data | ++--------+--------------------------------------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [-2678400000100, -5270400000000, -7948799999900] | ++--------+--------------------------------------------------+ + + +ast : a - interval b day +raw expr : substract_days(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) +checked expr : substract_days(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+----------------------------+---------+----------------------------+ +| | a | b | Output | ++--------+----------------------------+---------+----------------------------+ +| Type | Timestamp | Int32 | Timestamp | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-12-31 23:59:59.999900 | 1 | 1969-12-30 23:59:59.999900 | +| Row 1 | 1970-01-01 00:00:00.000000 | 2 | 1969-12-30 00:00:00.000000 | +| Row 2 | 1970-01-01 00:00:00.000100 | 3 | 1969-12-29 00:00:00.000100 | ++--------+----------------------------+---------+----------------------------+ +evaluation (internal): ++--------+----------------------------------------------+ +| Column | Data | ++--------+----------------------------------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [-86400000100, -172800000000, -259199999900] | ++--------+----------------------------------------------+ + + +ast : a - interval b hour +raw expr : substract_hours(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) +checked expr : substract_hours(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+----------------------------+---------+----------------------------+ +| | a | b | Output | ++--------+----------------------------+---------+----------------------------+ +| Type | Timestamp | Int32 | Timestamp | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-12-31 23:59:59.999900 | 1 | 1969-12-31 22:59:59.999900 | +| Row 1 | 1970-01-01 00:00:00.000000 | 2 | 1969-12-31 22:00:00.000000 | +| Row 2 | 1970-01-01 00:00:00.000100 | 3 | 1969-12-31 21:00:00.000100 | ++--------+----------------------------+---------+----------------------------+ +evaluation (internal): ++--------+------------------------------------------+ +| Column | Data | ++--------+------------------------------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [-3600000100, -7200000000, -10799999900] | ++--------+------------------------------------------+ + + +ast : a - interval b minute +raw expr : substract_minutes(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) +checked expr : substract_minutes(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+----------------------------+---------+----------------------------+ +| | a | b | Output | ++--------+----------------------------+---------+----------------------------+ +| Type | Timestamp | Int32 | Timestamp | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-12-31 23:59:59.999900 | 1 | 1969-12-31 23:58:59.999900 | +| Row 1 | 1970-01-01 00:00:00.000000 | 2 | 1969-12-31 23:58:00.000000 | +| Row 2 | 1970-01-01 00:00:00.000100 | 3 | 1969-12-31 23:57:00.000100 | ++--------+----------------------------+---------+----------------------------+ +evaluation (internal): ++--------+-------------------------------------+ +| Column | Data | ++--------+-------------------------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [-60000100, -120000000, -179999900] | ++--------+-------------------------------------+ + + +ast : a - interval b second +raw expr : substract_seconds(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) +checked expr : substract_seconds(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +evaluation: ++--------+----------------------------+---------+----------------------------+ +| | a | b | Output | ++--------+----------------------------+---------+----------------------------+ +| Type | Timestamp | Int32 | Timestamp | +| Domain | {-100..=100} | {1..=3} | Unknown | +| Row 0 | 1969-12-31 23:59:59.999900 | 1 | 1969-12-31 23:59:58.999900 | +| Row 1 | 1970-01-01 00:00:00.000000 | 2 | 1969-12-31 23:59:58.000000 | +| Row 2 | 1970-01-01 00:00:00.000100 | 3 | 1969-12-31 23:59:57.000100 | ++--------+----------------------------+---------+----------------------------+ +evaluation (internal): ++--------+--------------------------------+ +| Column | Data | ++--------+--------------------------------+ +| a | [-100, 0, 100] | +| b | Int32([1, 2, 3]) | +| Output | [-1000100, -2000000, -2999900] | ++--------+--------------------------------+ + + From cf6b9e7faef450f6e1591eda32f726eae20f0edb Mon Sep 17 00:00:00 2001 From: RinChanNOWWW Date: Sat, 15 Oct 2022 10:22:18 +0800 Subject: [PATCH 6/8] Make last day look up table static --- src/query/expression/src/date_helper.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/query/expression/src/date_helper.rs b/src/query/expression/src/date_helper.rs index 6ddcc59e7fa0..a929e7dd3dc7 100644 --- a/src/query/expression/src/date_helper.rs +++ b/src/query/expression/src/date_helper.rs @@ -55,6 +55,7 @@ where T: AsPrimitive pub const FACTOR_HOUR: i64 = 3600; pub const FACTOR_MINUTE: i64 = 60; pub const FACTOR_SECOND: i64 = 1; +const LAST_DAY_LUT: [u8; 13] = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; fn add_years_base(year: i32, month: u32, day: u32, delta: i64) -> Result { let new_year = year + delta as i32; @@ -97,8 +98,7 @@ fn last_day_of_year_month(year: i32, month: u32) -> u32 { if std::intrinsics::unlikely(month == 2 && is_leap_year) { return 29; } - let last_day_lookup = [0u32, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; - last_day_lookup[month as usize] + LAST_DAY_LUT[month as usize] as u32 } macro_rules! impl_interval_year_month { From 8f152fca2edf80a844720a8cfde20192d0db9642 Mon Sep 17 00:00:00 2001 From: RinChanNOWWW Date: Sat, 15 Oct 2022 10:45:37 +0800 Subject: [PATCH 7/8] Fix typo: substract -> subtract. --- .../functions-v2/src/scalars/arithmetic.rs | 2 +- .../functions-v2/src/scalars/datetime.rs | 2 +- .../functions-v2/tests/it/scalars/datetime.rs | 52 +-- .../functions-v2/tests/it/scalars/parser.rs | 4 +- .../tests/it/scalars/testdata/datetime.txt | 308 +++++++++--------- 5 files changed, 184 insertions(+), 184 deletions(-) diff --git a/src/query/functions-v2/src/scalars/arithmetic.rs b/src/query/functions-v2/src/scalars/arithmetic.rs index e789857ef2f0..659b72fe1ece 100644 --- a/src/query/functions-v2/src/scalars/arithmetic.rs +++ b/src/query/functions-v2/src/scalars/arithmetic.rs @@ -29,7 +29,7 @@ use super::arithmetic_modulo::vectorize_modulo; pub fn register(registry: &mut FunctionRegistry) { registry.register_aliases("plus", &["add"]); - registry.register_aliases("minus", &["substract", "neg"]); + registry.register_aliases("minus", &["subtract", "neg"]); registry.register_aliases("div", &["intdiv"]); // TODO support modulo diff --git a/src/query/functions-v2/src/scalars/datetime.rs b/src/query/functions-v2/src/scalars/datetime.rs index 1777eeb31581..f84259a85a77 100644 --- a/src/query/functions-v2/src/scalars/datetime.rs +++ b/src/query/functions-v2/src/scalars/datetime.rs @@ -400,4 +400,4 @@ macro_rules! impl_register_arith_functions { } impl_register_arith_functions!(register_add_functions, "add", unsigned_ident); -impl_register_arith_functions!(register_sub_functions, "substract", signed_ident); +impl_register_arith_functions!(register_sub_functions, "subtract", signed_ident); diff --git a/src/query/functions-v2/tests/it/scalars/datetime.rs b/src/query/functions-v2/tests/it/scalars/datetime.rs index 4702af6e27c5..755fccc499b5 100644 --- a/src/query/functions-v2/tests/it/scalars/datetime.rs +++ b/src/query/functions-v2/tests/it/scalars/datetime.rs @@ -33,8 +33,8 @@ fn test_datetime() { test_to_datetime(file); test_to_date(file); // {add | subtract}_{years | months | days | hours | minutes | seconds}(date, number) - test_date_add_substract(file); - test_timestamp_add_substract(file); + test_date_add_subtract(file); + test_timestamp_add_subtract(file); // date_{add | sub}({year | quarter | month | week | day | hour | minute | second}, date, number) test_date_date_add_sub(file); test_timestamp_date_add_sub(file); @@ -111,15 +111,15 @@ fn test_to_date(file: &mut impl Write) { )]); } -fn test_date_add_substract(file: &mut impl Write) { +fn test_date_add_subtract(file: &mut impl Write) { run_ast(file, "add_years(to_date(0), 10000)", &[]); // failed run_ast(file, "add_years(to_date(0), 100)", &[]); run_ast(file, "add_months(to_date(0), 100)", &[]); run_ast(file, "add_days(to_date(0), 100)", &[]); - run_ast(file, "substract_years(to_date(0), 100)", &[]); - run_ast(file, "substract_quarters(to_date(0), 100)", &[]); - run_ast(file, "substract_months(to_date(0), 100)", &[]); - run_ast(file, "substract_days(to_date(0), 100)", &[]); + run_ast(file, "subtract_years(to_date(0), 100)", &[]); + run_ast(file, "subtract_quarters(to_date(0), 100)", &[]); + run_ast(file, "subtract_months(to_date(0), 100)", &[]); + run_ast(file, "subtract_days(to_date(0), 100)", &[]); run_ast(file, "add_years(a, b)", &[ ("a", DataType::Date, from_date_data(vec![-100, 0, 100])), ( @@ -152,7 +152,7 @@ fn test_date_add_substract(file: &mut impl Write) { Column::from_data(vec![1, 2, 3]), ), ]); - run_ast(file, "substract_years(a, b)", &[ + run_ast(file, "subtract_years(a, b)", &[ ("a", DataType::Date, from_date_data(vec![-100, 0, 100])), ( "b", @@ -160,7 +160,7 @@ fn test_date_add_substract(file: &mut impl Write) { Column::from_data(vec![1, 2, 3]), ), ]); - run_ast(file, "substract_quarters(a, b)", &[ + run_ast(file, "subtract_quarters(a, b)", &[ ("a", DataType::Date, from_date_data(vec![-100, 0, 100])), ( "b", @@ -168,7 +168,7 @@ fn test_date_add_substract(file: &mut impl Write) { Column::from_data(vec![1, 2, 3]), ), ]); - run_ast(file, "substract_months(a, b)", &[ + run_ast(file, "subtract_months(a, b)", &[ ("a", DataType::Date, from_date_data(vec![-100, 0, 100])), ( "b", @@ -176,7 +176,7 @@ fn test_date_add_substract(file: &mut impl Write) { Column::from_data(vec![1, 2, 3]), ), ]); - run_ast(file, "substract_days(a, b)", &[ + run_ast(file, "subtract_days(a, b)", &[ ("a", DataType::Date, from_date_data(vec![-100, 0, 100])), ( "b", @@ -186,7 +186,7 @@ fn test_date_add_substract(file: &mut impl Write) { ]); } -fn test_timestamp_add_substract(file: &mut impl Write) { +fn test_timestamp_add_subtract(file: &mut impl Write) { run_ast(file, "add_years(to_timestamp(0), 10000)", &[]); // failed run_ast(file, "add_years(to_timestamp(0), 100)", &[]); run_ast(file, "add_quarters(to_timestamp(0), 100)", &[]); @@ -195,13 +195,13 @@ fn test_timestamp_add_substract(file: &mut impl Write) { run_ast(file, "add_hours(to_timestamp(0), 100)", &[]); run_ast(file, "add_minutes(to_timestamp(0), 100)", &[]); run_ast(file, "add_seconds(to_timestamp(0), 100)", &[]); - run_ast(file, "substract_years(to_timestamp(0), 100)", &[]); - run_ast(file, "substract_quarters(to_timestamp(0), 100)", &[]); - run_ast(file, "substract_months(to_timestamp(0), 100)", &[]); - run_ast(file, "substract_days(to_timestamp(0), 100)", &[]); - run_ast(file, "substract_hours(to_timestamp(0), 100)", &[]); - run_ast(file, "substract_minutes(to_timestamp(0), 100)", &[]); - run_ast(file, "substract_seconds(to_timestamp(0), 100)", &[]); + run_ast(file, "subtract_years(to_timestamp(0), 100)", &[]); + run_ast(file, "subtract_quarters(to_timestamp(0), 100)", &[]); + run_ast(file, "subtract_months(to_timestamp(0), 100)", &[]); + run_ast(file, "subtract_days(to_timestamp(0), 100)", &[]); + run_ast(file, "subtract_hours(to_timestamp(0), 100)", &[]); + run_ast(file, "subtract_minutes(to_timestamp(0), 100)", &[]); + run_ast(file, "subtract_seconds(to_timestamp(0), 100)", &[]); run_ast(file, "add_years(a, b)", &[ ( "a", @@ -286,7 +286,7 @@ fn test_timestamp_add_substract(file: &mut impl Write) { Column::from_data(vec![1, 2, 3]), ), ]); - run_ast(file, "substract_years(a, b)", &[ + run_ast(file, "subtract_years(a, b)", &[ ( "a", DataType::Timestamp, @@ -298,7 +298,7 @@ fn test_timestamp_add_substract(file: &mut impl Write) { Column::from_data(vec![1, 2, 3]), ), ]); - run_ast(file, "substract_quarters(a, b)", &[ + run_ast(file, "subtract_quarters(a, b)", &[ ( "a", DataType::Timestamp, @@ -310,7 +310,7 @@ fn test_timestamp_add_substract(file: &mut impl Write) { Column::from_data(vec![1, 2, 3]), ), ]); - run_ast(file, "substract_months(a, b)", &[ + run_ast(file, "subtract_months(a, b)", &[ ( "a", DataType::Timestamp, @@ -322,7 +322,7 @@ fn test_timestamp_add_substract(file: &mut impl Write) { Column::from_data(vec![1, 2, 3]), ), ]); - run_ast(file, "substract_days(a, b)", &[ + run_ast(file, "subtract_days(a, b)", &[ ( "a", DataType::Timestamp, @@ -334,7 +334,7 @@ fn test_timestamp_add_substract(file: &mut impl Write) { Column::from_data(vec![1, 2, 3]), ), ]); - run_ast(file, "substract_hours(a, b)", &[ + run_ast(file, "subtract_hours(a, b)", &[ ( "a", DataType::Timestamp, @@ -346,7 +346,7 @@ fn test_timestamp_add_substract(file: &mut impl Write) { Column::from_data(vec![1, 2, 3]), ), ]); - run_ast(file, "substract_minutes(a, b)", &[ + run_ast(file, "subtract_minutes(a, b)", &[ ( "a", DataType::Timestamp, @@ -358,7 +358,7 @@ fn test_timestamp_add_substract(file: &mut impl Write) { Column::from_data(vec![1, 2, 3]), ), ]); - run_ast(file, "substract_seconds(a, b)", &[ + run_ast(file, "subtract_seconds(a, b)", &[ ( "a", DataType::Timestamp, diff --git a/src/query/functions-v2/tests/it/scalars/parser.rs b/src/query/functions-v2/tests/it/scalars/parser.rs index 19ed2027aa1a..26ffcbc5677a 100644 --- a/src/query/functions-v2/tests/it/scalars/parser.rs +++ b/src/query/functions-v2/tests/it/scalars/parser.rs @@ -68,7 +68,7 @@ macro_rules! transform_interval_add_sub { with_interval_mapped_name!(|INTERVAL| match $unit { IntervalKind::INTERVAL => RawExpr::FunctionCall { span: transform_span($span), - name: concat!("substract_", INTERVAL).to_string(), + name: concat!("subtract_", INTERVAL).to_string(), params: vec![], args: vec![ transform_expr(*$date, $columns), @@ -383,7 +383,7 @@ pub fn transform_expr(ast: common_ast::ast::Expr, columns: &[(&str, DataType)]) with_interval_mapped_name!(|INTERVAL| match unit { IntervalKind::INTERVAL => RawExpr::FunctionCall { span: transform_span(span), - name: concat!("substract_", INTERVAL).to_string(), + name: concat!("subtract_", INTERVAL).to_string(), params: vec![], args: vec![ transform_expr(*date, columns), diff --git a/src/query/functions-v2/tests/it/scalars/testdata/datetime.txt b/src/query/functions-v2/tests/it/scalars/testdata/datetime.txt index 2f6f0f63c7f2..aa6813e7686e 100644 --- a/src/query/functions-v2/tests/it/scalars/testdata/datetime.txt +++ b/src/query/functions-v2/tests/it/scalars/testdata/datetime.txt @@ -355,36 +355,36 @@ output domain : Unknown output : 1970-04-11 -ast : substract_years(to_date(0), 100) -raw expr : substract_years(to_date(0_u8), 100_u8) -checked expr : substract_years(to_date(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +ast : subtract_years(to_date(0), 100) +raw expr : subtract_years(to_date(0_u8), 100_u8) +checked expr : subtract_years(to_date(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) optimized expr : -36524 output type : Date output domain : Unknown output : 1870-01-01 -ast : substract_quarters(to_date(0), 100) -raw expr : substract_quarters(to_date(0_u8), 100_u8) -checked expr : substract_quarters(to_date(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +ast : subtract_quarters(to_date(0), 100) +raw expr : subtract_quarters(to_date(0_u8), 100_u8) +checked expr : subtract_quarters(to_date(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) optimized expr : -9131 output type : Date output domain : Unknown output : 1945-01-01 -ast : substract_months(to_date(0), 100) -raw expr : substract_months(to_date(0_u8), 100_u8) -checked expr : substract_months(to_date(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +ast : subtract_months(to_date(0), 100) +raw expr : subtract_months(to_date(0_u8), 100_u8) +checked expr : subtract_months(to_date(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) optimized expr : -3044 output type : Date output domain : Unknown output : 1961-09-01 -ast : substract_days(to_date(0), 100) -raw expr : substract_days(to_date(0_u8), 100_u8) -checked expr : substract_days(to_date(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +ast : subtract_days(to_date(0), 100) +raw expr : subtract_days(to_date(0_u8), 100_u8) +checked expr : subtract_days(to_date(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) optimized expr : -100 output type : Date output domain : Unknown @@ -483,9 +483,9 @@ evaluation (internal): +--------+------------------+ -ast : substract_years(a, b) -raw expr : substract_years(ColumnRef(0)::Date, ColumnRef(1)::Int32) -checked expr : substract_years(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +ast : subtract_years(a, b) +raw expr : subtract_years(ColumnRef(0)::Date, ColumnRef(1)::Int32) +checked expr : subtract_years(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) evaluation: +--------+--------------+---------+------------+ | | a | b | Output | @@ -506,9 +506,9 @@ evaluation (internal): +--------+--------------------+ -ast : substract_quarters(a, b) -raw expr : substract_quarters(ColumnRef(0)::Date, ColumnRef(1)::Int32) -checked expr : substract_quarters(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +ast : subtract_quarters(a, b) +raw expr : subtract_quarters(ColumnRef(0)::Date, ColumnRef(1)::Int32) +checked expr : subtract_quarters(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) evaluation: +--------+--------------+---------+------------+ | | a | b | Output | @@ -529,9 +529,9 @@ evaluation (internal): +--------+--------------------+ -ast : substract_months(a, b) -raw expr : substract_months(ColumnRef(0)::Date, ColumnRef(1)::Int32) -checked expr : substract_months(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +ast : subtract_months(a, b) +raw expr : subtract_months(ColumnRef(0)::Date, ColumnRef(1)::Int32) +checked expr : subtract_months(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) evaluation: +--------+--------------+---------+------------+ | | a | b | Output | @@ -552,9 +552,9 @@ evaluation (internal): +--------+------------------+ -ast : substract_days(a, b) -raw expr : substract_days(ColumnRef(0)::Date, ColumnRef(1)::Int32) -checked expr : substract_days(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +ast : subtract_days(a, b) +raw expr : subtract_days(ColumnRef(0)::Date, ColumnRef(1)::Int32) +checked expr : subtract_days(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) evaluation: +--------+--------------+---------+------------+ | | a | b | Output | @@ -646,63 +646,63 @@ output domain : Unknown output : 1970-01-01 00:01:40.000000 -ast : substract_years(to_timestamp(0), 100) -raw expr : substract_years(to_timestamp(0_u8), 100_u8) -checked expr : substract_years(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +ast : subtract_years(to_timestamp(0), 100) +raw expr : subtract_years(to_timestamp(0_u8), 100_u8) +checked expr : subtract_years(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) optimized expr : -3155673600000000 output type : Timestamp output domain : Unknown output : 1870-01-01 00:00:00.000000 -ast : substract_quarters(to_timestamp(0), 100) -raw expr : substract_quarters(to_timestamp(0_u8), 100_u8) -checked expr : substract_quarters(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +ast : subtract_quarters(to_timestamp(0), 100) +raw expr : subtract_quarters(to_timestamp(0_u8), 100_u8) +checked expr : subtract_quarters(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) optimized expr : -788918400000000 output type : Timestamp output domain : Unknown output : 1945-01-01 00:00:00.000000 -ast : substract_months(to_timestamp(0), 100) -raw expr : substract_months(to_timestamp(0_u8), 100_u8) -checked expr : substract_months(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +ast : subtract_months(to_timestamp(0), 100) +raw expr : subtract_months(to_timestamp(0_u8), 100_u8) +checked expr : subtract_months(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) optimized expr : -263001600000000 output type : Timestamp output domain : Unknown output : 1961-09-01 00:00:00.000000 -ast : substract_days(to_timestamp(0), 100) -raw expr : substract_days(to_timestamp(0_u8), 100_u8) -checked expr : substract_days(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +ast : subtract_days(to_timestamp(0), 100) +raw expr : subtract_days(to_timestamp(0_u8), 100_u8) +checked expr : subtract_days(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) optimized expr : -8640000000000 output type : Timestamp output domain : Unknown output : 1969-09-23 00:00:00.000000 -ast : substract_hours(to_timestamp(0), 100) -raw expr : substract_hours(to_timestamp(0_u8), 100_u8) -checked expr : substract_hours(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +ast : subtract_hours(to_timestamp(0), 100) +raw expr : subtract_hours(to_timestamp(0_u8), 100_u8) +checked expr : subtract_hours(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) optimized expr : -360000000000 output type : Timestamp output domain : Unknown output : 1969-12-27 20:00:00.000000 -ast : substract_minutes(to_timestamp(0), 100) -raw expr : substract_minutes(to_timestamp(0_u8), 100_u8) -checked expr : substract_minutes(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +ast : subtract_minutes(to_timestamp(0), 100) +raw expr : subtract_minutes(to_timestamp(0_u8), 100_u8) +checked expr : subtract_minutes(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) optimized expr : -6000000000 output type : Timestamp output domain : Unknown output : 1969-12-31 22:20:00.000000 -ast : substract_seconds(to_timestamp(0), 100) -raw expr : substract_seconds(to_timestamp(0_u8), 100_u8) -checked expr : substract_seconds(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +ast : subtract_seconds(to_timestamp(0), 100) +raw expr : subtract_seconds(to_timestamp(0_u8), 100_u8) +checked expr : subtract_seconds(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) optimized expr : -100000000 output type : Timestamp output domain : Unknown @@ -870,9 +870,9 @@ evaluation (internal): +--------+----------------------------+ -ast : substract_years(a, b) -raw expr : substract_years(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) -checked expr : substract_years(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +ast : subtract_years(a, b) +raw expr : subtract_years(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) +checked expr : subtract_years(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) evaluation: +--------+----------------------------+---------+----------------------------+ | | a | b | Output | @@ -893,9 +893,9 @@ evaluation (internal): +--------+-----------------------------------------------------+ -ast : substract_quarters(a, b) -raw expr : substract_quarters(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) -checked expr : substract_quarters(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +ast : subtract_quarters(a, b) +raw expr : subtract_quarters(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) +checked expr : subtract_quarters(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) evaluation: +--------+----------------------------+---------+----------------------------+ | | a | b | Output | @@ -916,9 +916,9 @@ evaluation (internal): +--------+----------------------------------------------------+ -ast : substract_months(a, b) -raw expr : substract_months(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) -checked expr : substract_months(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +ast : subtract_months(a, b) +raw expr : subtract_months(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) +checked expr : subtract_months(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) evaluation: +--------+----------------------------+---------+----------------------------+ | | a | b | Output | @@ -939,9 +939,9 @@ evaluation (internal): +--------+--------------------------------------------------+ -ast : substract_days(a, b) -raw expr : substract_days(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) -checked expr : substract_days(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +ast : subtract_days(a, b) +raw expr : subtract_days(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) +checked expr : subtract_days(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) evaluation: +--------+----------------------------+---------+----------------------------+ | | a | b | Output | @@ -962,9 +962,9 @@ evaluation (internal): +--------+----------------------------------------------+ -ast : substract_hours(a, b) -raw expr : substract_hours(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) -checked expr : substract_hours(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +ast : subtract_hours(a, b) +raw expr : subtract_hours(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) +checked expr : subtract_hours(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) evaluation: +--------+----------------------------+---------+----------------------------+ | | a | b | Output | @@ -985,9 +985,9 @@ evaluation (internal): +--------+------------------------------------------+ -ast : substract_minutes(a, b) -raw expr : substract_minutes(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) -checked expr : substract_minutes(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +ast : subtract_minutes(a, b) +raw expr : subtract_minutes(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) +checked expr : subtract_minutes(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) evaluation: +--------+----------------------------+---------+----------------------------+ | | a | b | Output | @@ -1008,9 +1008,9 @@ evaluation (internal): +--------+-------------------------------------+ -ast : substract_seconds(a, b) -raw expr : substract_seconds(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) -checked expr : substract_seconds(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +ast : subtract_seconds(a, b) +raw expr : subtract_seconds(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) +checked expr : subtract_seconds(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) evaluation: +--------+----------------------------+---------+----------------------------+ | | a | b | Output | @@ -1076,8 +1076,8 @@ output : 1970-04-11 ast : date_sub(year, 100, to_date(0)) -raw expr : substract_years(to_date(0_u8), 100_u8) -checked expr : substract_years(to_date(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +raw expr : subtract_years(to_date(0_u8), 100_u8) +checked expr : subtract_years(to_date(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) optimized expr : -36524 output type : Date output domain : Unknown @@ -1085,8 +1085,8 @@ output : 1870-01-01 ast : date_sub(quarter, 100, to_date(0)) -raw expr : substract_quarters(to_date(0_u8), 100_u8) -checked expr : substract_quarters(to_date(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +raw expr : subtract_quarters(to_date(0_u8), 100_u8) +checked expr : subtract_quarters(to_date(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) optimized expr : -9131 output type : Date output domain : Unknown @@ -1094,8 +1094,8 @@ output : 1945-01-01 ast : date_sub(month, 100, to_date(0)) -raw expr : substract_months(to_date(0_u8), 100_u8) -checked expr : substract_months(to_date(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +raw expr : subtract_months(to_date(0_u8), 100_u8) +checked expr : subtract_months(to_date(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) optimized expr : -3044 output type : Date output domain : Unknown @@ -1103,8 +1103,8 @@ output : 1961-09-01 ast : date_sub(day, 100, to_date(0)) -raw expr : substract_days(to_date(0_u8), 100_u8) -checked expr : substract_days(to_date(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +raw expr : subtract_days(to_date(0_u8), 100_u8) +checked expr : subtract_days(to_date(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) optimized expr : -100 output type : Date output domain : Unknown @@ -1204,8 +1204,8 @@ evaluation (internal): ast : date_sub(year, b, a) -raw expr : substract_years(ColumnRef(0)::Date, ColumnRef(1)::Int32) -checked expr : substract_years(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +raw expr : subtract_years(ColumnRef(0)::Date, ColumnRef(1)::Int32) +checked expr : subtract_years(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) evaluation: +--------+--------------+---------+------------+ | | a | b | Output | @@ -1227,8 +1227,8 @@ evaluation (internal): ast : date_sub(quarter, b, a) -raw expr : substract_quarters(ColumnRef(0)::Date, ColumnRef(1)::Int32) -checked expr : substract_quarters(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +raw expr : subtract_quarters(ColumnRef(0)::Date, ColumnRef(1)::Int32) +checked expr : subtract_quarters(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) evaluation: +--------+--------------+---------+------------+ | | a | b | Output | @@ -1250,8 +1250,8 @@ evaluation (internal): ast : date_sub(month, b, a) -raw expr : substract_months(ColumnRef(0)::Date, ColumnRef(1)::Int32) -checked expr : substract_months(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +raw expr : subtract_months(ColumnRef(0)::Date, ColumnRef(1)::Int32) +checked expr : subtract_months(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) evaluation: +--------+--------------+---------+------------+ | | a | b | Output | @@ -1273,8 +1273,8 @@ evaluation (internal): ast : date_sub(day, b, a) -raw expr : substract_days(ColumnRef(0)::Date, ColumnRef(1)::Int32) -checked expr : substract_days(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +raw expr : subtract_days(ColumnRef(0)::Date, ColumnRef(1)::Int32) +checked expr : subtract_days(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) evaluation: +--------+--------------+---------+------------+ | | a | b | Output | @@ -1367,8 +1367,8 @@ output : 1970-01-01 00:01:40.000000 ast : date_sub(year, 100, to_timestamp(0)) -raw expr : substract_years(to_timestamp(0_u8), 100_u8) -checked expr : substract_years(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +raw expr : subtract_years(to_timestamp(0_u8), 100_u8) +checked expr : subtract_years(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) optimized expr : -3155673600000000 output type : Timestamp output domain : Unknown @@ -1376,8 +1376,8 @@ output : 1870-01-01 00:00:00.000000 ast : date_sub(quarter, 100, to_timestamp(0)) -raw expr : substract_quarters(to_timestamp(0_u8), 100_u8) -checked expr : substract_quarters(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +raw expr : subtract_quarters(to_timestamp(0_u8), 100_u8) +checked expr : subtract_quarters(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) optimized expr : -788918400000000 output type : Timestamp output domain : Unknown @@ -1385,8 +1385,8 @@ output : 1945-01-01 00:00:00.000000 ast : date_sub(month, 100, to_timestamp(0)) -raw expr : substract_months(to_timestamp(0_u8), 100_u8) -checked expr : substract_months(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +raw expr : subtract_months(to_timestamp(0_u8), 100_u8) +checked expr : subtract_months(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) optimized expr : -263001600000000 output type : Timestamp output domain : Unknown @@ -1394,8 +1394,8 @@ output : 1961-09-01 00:00:00.000000 ast : date_sub(day, 100, to_timestamp(0)) -raw expr : substract_days(to_timestamp(0_u8), 100_u8) -checked expr : substract_days(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +raw expr : subtract_days(to_timestamp(0_u8), 100_u8) +checked expr : subtract_days(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) optimized expr : -8640000000000 output type : Timestamp output domain : Unknown @@ -1403,8 +1403,8 @@ output : 1969-09-23 00:00:00.000000 ast : date_sub(hour, 100, to_timestamp(0)) -raw expr : substract_hours(to_timestamp(0_u8), 100_u8) -checked expr : substract_hours(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +raw expr : subtract_hours(to_timestamp(0_u8), 100_u8) +checked expr : subtract_hours(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) optimized expr : -360000000000 output type : Timestamp output domain : Unknown @@ -1412,8 +1412,8 @@ output : 1969-12-27 20:00:00.000000 ast : date_sub(minute, 100, to_timestamp(0)) -raw expr : substract_minutes(to_timestamp(0_u8), 100_u8) -checked expr : substract_minutes(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +raw expr : subtract_minutes(to_timestamp(0_u8), 100_u8) +checked expr : subtract_minutes(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) optimized expr : -6000000000 output type : Timestamp output domain : Unknown @@ -1421,8 +1421,8 @@ output : 1969-12-31 22:20:00.000000 ast : date_sub(second, 100, to_timestamp(0)) -raw expr : substract_seconds(to_timestamp(0_u8), 100_u8) -checked expr : substract_seconds(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +raw expr : subtract_seconds(to_timestamp(0_u8), 100_u8) +checked expr : subtract_seconds(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) optimized expr : -100000000 output type : Timestamp output domain : Unknown @@ -1591,8 +1591,8 @@ evaluation (internal): ast : date_sub(year, b, a) -raw expr : substract_years(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) -checked expr : substract_years(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +raw expr : subtract_years(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) +checked expr : subtract_years(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) evaluation: +--------+----------------------------+---------+----------------------------+ | | a | b | Output | @@ -1614,8 +1614,8 @@ evaluation (internal): ast : date_sub(quarter, b, a) -raw expr : substract_quarters(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) -checked expr : substract_quarters(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +raw expr : subtract_quarters(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) +checked expr : subtract_quarters(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) evaluation: +--------+----------------------------+---------+----------------------------+ | | a | b | Output | @@ -1637,8 +1637,8 @@ evaluation (internal): ast : date_sub(month, b, a) -raw expr : substract_months(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) -checked expr : substract_months(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +raw expr : subtract_months(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) +checked expr : subtract_months(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) evaluation: +--------+----------------------------+---------+----------------------------+ | | a | b | Output | @@ -1660,8 +1660,8 @@ evaluation (internal): ast : date_sub(day, b, a) -raw expr : substract_days(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) -checked expr : substract_days(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +raw expr : subtract_days(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) +checked expr : subtract_days(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) evaluation: +--------+----------------------------+---------+----------------------------+ | | a | b | Output | @@ -1683,8 +1683,8 @@ evaluation (internal): ast : date_sub(hour, b, a) -raw expr : substract_hours(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) -checked expr : substract_hours(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +raw expr : subtract_hours(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) +checked expr : subtract_hours(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) evaluation: +--------+----------------------------+---------+----------------------------+ | | a | b | Output | @@ -1706,8 +1706,8 @@ evaluation (internal): ast : date_sub(minute, b, a) -raw expr : substract_minutes(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) -checked expr : substract_minutes(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +raw expr : subtract_minutes(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) +checked expr : subtract_minutes(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) evaluation: +--------+----------------------------+---------+----------------------------+ | | a | b | Output | @@ -1729,8 +1729,8 @@ evaluation (internal): ast : date_sub(second, b, a) -raw expr : substract_seconds(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) -checked expr : substract_seconds(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +raw expr : subtract_seconds(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) +checked expr : subtract_seconds(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) evaluation: +--------+----------------------------+---------+----------------------------+ | | a | b | Output | @@ -1796,8 +1796,8 @@ output : 1970-04-11 ast : to_date(0) - interval 100 year -raw expr : substract_years(to_date(0_u8), 100_u8) -checked expr : substract_years(to_date(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +raw expr : subtract_years(to_date(0_u8), 100_u8) +checked expr : subtract_years(to_date(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) optimized expr : -36524 output type : Date output domain : Unknown @@ -1805,8 +1805,8 @@ output : 1870-01-01 ast : to_date(0) - interval 100 quarter -raw expr : substract_quarters(to_date(0_u8), 100_u8) -checked expr : substract_quarters(to_date(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +raw expr : subtract_quarters(to_date(0_u8), 100_u8) +checked expr : subtract_quarters(to_date(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) optimized expr : -9131 output type : Date output domain : Unknown @@ -1814,8 +1814,8 @@ output : 1945-01-01 ast : to_date(0) - interval 100 month -raw expr : substract_months(to_date(0_u8), 100_u8) -checked expr : substract_months(to_date(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +raw expr : subtract_months(to_date(0_u8), 100_u8) +checked expr : subtract_months(to_date(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) optimized expr : -3044 output type : Date output domain : Unknown @@ -1823,8 +1823,8 @@ output : 1961-09-01 ast : to_date(0) - interval 100 day -raw expr : substract_days(to_date(0_u8), 100_u8) -checked expr : substract_days(to_date(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +raw expr : subtract_days(to_date(0_u8), 100_u8) +checked expr : subtract_days(to_date(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) optimized expr : -100 output type : Date output domain : Unknown @@ -1924,8 +1924,8 @@ evaluation (internal): ast : a - interval b year -raw expr : substract_years(ColumnRef(0)::Date, ColumnRef(1)::Int32) -checked expr : substract_years(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +raw expr : subtract_years(ColumnRef(0)::Date, ColumnRef(1)::Int32) +checked expr : subtract_years(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) evaluation: +--------+--------------+---------+------------+ | | a | b | Output | @@ -1947,8 +1947,8 @@ evaluation (internal): ast : a - interval b quarter -raw expr : substract_quarters(ColumnRef(0)::Date, ColumnRef(1)::Int32) -checked expr : substract_quarters(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +raw expr : subtract_quarters(ColumnRef(0)::Date, ColumnRef(1)::Int32) +checked expr : subtract_quarters(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) evaluation: +--------+--------------+---------+------------+ | | a | b | Output | @@ -1970,8 +1970,8 @@ evaluation (internal): ast : a - interval b month -raw expr : substract_months(ColumnRef(0)::Date, ColumnRef(1)::Int32) -checked expr : substract_months(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +raw expr : subtract_months(ColumnRef(0)::Date, ColumnRef(1)::Int32) +checked expr : subtract_months(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) evaluation: +--------+--------------+---------+------------+ | | a | b | Output | @@ -1993,8 +1993,8 @@ evaluation (internal): ast : a - interval b day -raw expr : substract_days(ColumnRef(0)::Date, ColumnRef(1)::Int32) -checked expr : substract_days(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +raw expr : subtract_days(ColumnRef(0)::Date, ColumnRef(1)::Int32) +checked expr : subtract_days(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) evaluation: +--------+--------------+---------+------------+ | | a | b | Output | @@ -2087,8 +2087,8 @@ output : 1970-01-01 00:01:40.000000 ast : to_timestamp(0) - interval 100 year -raw expr : substract_years(to_timestamp(0_u8), 100_u8) -checked expr : substract_years(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +raw expr : subtract_years(to_timestamp(0_u8), 100_u8) +checked expr : subtract_years(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) optimized expr : -3155673600000000 output type : Timestamp output domain : Unknown @@ -2096,8 +2096,8 @@ output : 1870-01-01 00:00:00.000000 ast : to_timestamp(0) - interval 100 quarter -raw expr : substract_quarters(to_timestamp(0_u8), 100_u8) -checked expr : substract_quarters(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +raw expr : subtract_quarters(to_timestamp(0_u8), 100_u8) +checked expr : subtract_quarters(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) optimized expr : -788918400000000 output type : Timestamp output domain : Unknown @@ -2105,8 +2105,8 @@ output : 1945-01-01 00:00:00.000000 ast : to_timestamp(0) - interval 100 month -raw expr : substract_months(to_timestamp(0_u8), 100_u8) -checked expr : substract_months(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +raw expr : subtract_months(to_timestamp(0_u8), 100_u8) +checked expr : subtract_months(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) optimized expr : -263001600000000 output type : Timestamp output domain : Unknown @@ -2114,8 +2114,8 @@ output : 1961-09-01 00:00:00.000000 ast : to_timestamp(0) - interval 100 day -raw expr : substract_days(to_timestamp(0_u8), 100_u8) -checked expr : substract_days(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +raw expr : subtract_days(to_timestamp(0_u8), 100_u8) +checked expr : subtract_days(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) optimized expr : -8640000000000 output type : Timestamp output domain : Unknown @@ -2123,8 +2123,8 @@ output : 1969-09-23 00:00:00.000000 ast : to_timestamp(0) - interval 100 hour -raw expr : substract_hours(to_timestamp(0_u8), 100_u8) -checked expr : substract_hours(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +raw expr : subtract_hours(to_timestamp(0_u8), 100_u8) +checked expr : subtract_hours(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) optimized expr : -360000000000 output type : Timestamp output domain : Unknown @@ -2132,8 +2132,8 @@ output : 1969-12-27 20:00:00.000000 ast : to_timestamp(0) - interval 100 minute -raw expr : substract_minutes(to_timestamp(0_u8), 100_u8) -checked expr : substract_minutes(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +raw expr : subtract_minutes(to_timestamp(0_u8), 100_u8) +checked expr : subtract_minutes(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) optimized expr : -6000000000 output type : Timestamp output domain : Unknown @@ -2141,8 +2141,8 @@ output : 1969-12-31 22:20:00.000000 ast : to_timestamp(0) - interval 100 second -raw expr : substract_seconds(to_timestamp(0_u8), 100_u8) -checked expr : substract_seconds(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) +raw expr : subtract_seconds(to_timestamp(0_u8), 100_u8) +checked expr : subtract_seconds(to_timestamp(CAST(0_u8 AS Int64)), CAST(100_u8 AS Int64)) optimized expr : -100000000 output type : Timestamp output domain : Unknown @@ -2311,8 +2311,8 @@ evaluation (internal): ast : a - interval b year -raw expr : substract_years(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) -checked expr : substract_years(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +raw expr : subtract_years(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) +checked expr : subtract_years(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) evaluation: +--------+----------------------------+---------+----------------------------+ | | a | b | Output | @@ -2334,8 +2334,8 @@ evaluation (internal): ast : a - interval b quarter -raw expr : substract_quarters(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) -checked expr : substract_quarters(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +raw expr : subtract_quarters(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) +checked expr : subtract_quarters(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) evaluation: +--------+----------------------------+---------+----------------------------+ | | a | b | Output | @@ -2357,8 +2357,8 @@ evaluation (internal): ast : a - interval b month -raw expr : substract_months(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) -checked expr : substract_months(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +raw expr : subtract_months(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) +checked expr : subtract_months(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) evaluation: +--------+----------------------------+---------+----------------------------+ | | a | b | Output | @@ -2380,8 +2380,8 @@ evaluation (internal): ast : a - interval b day -raw expr : substract_days(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) -checked expr : substract_days(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +raw expr : subtract_days(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) +checked expr : subtract_days(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) evaluation: +--------+----------------------------+---------+----------------------------+ | | a | b | Output | @@ -2403,8 +2403,8 @@ evaluation (internal): ast : a - interval b hour -raw expr : substract_hours(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) -checked expr : substract_hours(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +raw expr : subtract_hours(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) +checked expr : subtract_hours(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) evaluation: +--------+----------------------------+---------+----------------------------+ | | a | b | Output | @@ -2426,8 +2426,8 @@ evaluation (internal): ast : a - interval b minute -raw expr : substract_minutes(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) -checked expr : substract_minutes(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +raw expr : subtract_minutes(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) +checked expr : subtract_minutes(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) evaluation: +--------+----------------------------+---------+----------------------------+ | | a | b | Output | @@ -2449,8 +2449,8 @@ evaluation (internal): ast : a - interval b second -raw expr : substract_seconds(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) -checked expr : substract_seconds(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) +raw expr : subtract_seconds(ColumnRef(0)::Timestamp, ColumnRef(1)::Int32) +checked expr : subtract_seconds(ColumnRef(0), CAST(ColumnRef(1) AS Int64)) evaluation: +--------+----------------------------+---------+----------------------------+ | | a | b | Output | From e8a529106e1f2ca75205c1e814e4e624d2c92c99 Mon Sep 17 00:00:00 2001 From: RinChanNOWWW Date: Sun, 16 Oct 2022 11:06:22 +0800 Subject: [PATCH 8/8] Migrate `IntervalKind` to `common_ast::ast`. --- Cargo.lock | 2 - src/query/ast/Cargo.toml | 1 - src/query/ast/src/ast/expr.rs | 30 +++++++++- src/query/ast/src/ast/format/ast_format.rs | 1 - src/query/ast/src/parser/expr.rs | 1 - src/query/ast/src/visitors/visitor.rs | 1 - src/query/ast/src/visitors/visitor_mut.rs | 1 - src/query/functions-v2/Cargo.toml | 1 - .../functions-v2/tests/it/scalars/parser.rs | 2 +- .../src/sql/planner/semantic/type_check.rs | 59 ++++++++++--------- 10 files changed, 60 insertions(+), 39 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2d22144a6c29..c90e3b6b89d7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1182,7 +1182,6 @@ version = "0.1.0" dependencies = [ "codespan-reporting", "common-base", - "common-datavalues", "common-exception", "common-functions", "common-meta-app", @@ -1462,7 +1461,6 @@ dependencies = [ "comfy-table", "common-arrow", "common-ast", - "common-datavalues", "common-exception", "common-expression", "common-hashtable", diff --git a/src/query/ast/Cargo.toml b/src/query/ast/Cargo.toml index aee67bc0345a..5f1f2f2fbb21 100644 --- a/src/query/ast/Cargo.toml +++ b/src/query/ast/Cargo.toml @@ -11,7 +11,6 @@ doctest = false [dependencies] # In alphabetical order # Workspace dependencies -common-datavalues = { path = "../datavalues" } common-exception = { path = "../../common/exception" } common-functions = { path = "../functions" } common-meta-app = { path = "../../meta/app" } diff --git a/src/query/ast/src/ast/expr.rs b/src/query/ast/src/ast/expr.rs index 88ded21bc4aa..455be687b04f 100644 --- a/src/query/ast/src/ast/expr.rs +++ b/src/query/ast/src/ast/expr.rs @@ -15,7 +15,6 @@ use std::fmt::Display; use std::fmt::Formatter; -use common_datavalues::IntervalKind; use common_exception::ErrorCode; use common_exception::Result; @@ -25,6 +24,19 @@ use crate::ast::Identifier; use crate::ast::Query; use crate::parser::token::Token; +#[derive(Debug, Copy, Clone, PartialEq, Eq)] +pub enum IntervalKind { + Year, + Quarter, + Month, + Day, + Hour, + Minute, + Second, + Doy, + Dow, +} + #[derive(Debug, Clone, PartialEq)] pub enum Expr<'a> { /// Column reference, with indirection like `table.column` @@ -363,6 +375,22 @@ impl<'a> Expr<'a> { } } +impl Display for IntervalKind { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + f.write_str(match self { + IntervalKind::Year => "YEAR", + IntervalKind::Quarter => "QUARTER", + IntervalKind::Month => "MONTH", + IntervalKind::Day => "DAY", + IntervalKind::Hour => "HOUR", + IntervalKind::Minute => "MINUTE", + IntervalKind::Second => "SECOND", + IntervalKind::Doy => "DOY", + IntervalKind::Dow => "DOW", + }) + } +} + impl Display for SubqueryModifier { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { match self { diff --git a/src/query/ast/src/ast/format/ast_format.rs b/src/query/ast/src/ast/format/ast_format.rs index 74bccf5507ed..4ca91d1b27a9 100644 --- a/src/query/ast/src/ast/format/ast_format.rs +++ b/src/query/ast/src/ast/format/ast_format.rs @@ -14,7 +14,6 @@ use std::fmt::Display; -use common_datavalues::IntervalKind; use common_exception::Result; use common_meta_types::PrincipalIdentity; use common_meta_types::UserIdentity; diff --git a/src/query/ast/src/parser/expr.rs b/src/query/ast/src/parser/expr.rs index 0202ca03b3c5..e2e326dd8e6d 100644 --- a/src/query/ast/src/parser/expr.rs +++ b/src/query/ast/src/parser/expr.rs @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -use common_datavalues::IntervalKind; use itertools::Itertools; use nom::branch::alt; use nom::combinator::consumed; diff --git a/src/query/ast/src/visitors/visitor.rs b/src/query/ast/src/visitors/visitor.rs index f8aedaf8ec5f..2639ede64cf7 100644 --- a/src/query/ast/src/visitors/visitor.rs +++ b/src/query/ast/src/visitors/visitor.rs @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -use common_datavalues::IntervalKind; use common_meta_types::PrincipalIdentity; use common_meta_types::UserIdentity; diff --git a/src/query/ast/src/visitors/visitor_mut.rs b/src/query/ast/src/visitors/visitor_mut.rs index 6d9814fcac3b..89c3828d1632 100644 --- a/src/query/ast/src/visitors/visitor_mut.rs +++ b/src/query/ast/src/visitors/visitor_mut.rs @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -use common_datavalues::IntervalKind; use common_meta_types::PrincipalIdentity; use common_meta_types::UserIdentity; diff --git a/src/query/functions-v2/Cargo.toml b/src/query/functions-v2/Cargo.toml index f694203dd7cf..c5347967386c 100644 --- a/src/query/functions-v2/Cargo.toml +++ b/src/query/functions-v2/Cargo.toml @@ -12,7 +12,6 @@ doctest = false [dependencies] # In alphabetical order # Workspace dependencies common-arrow = { path = "../../common/arrow" } -common-datavalues = { path = "../datavalues" } common-exception = { path = "../../common/exception" } common-expression = { path = "../expression" } common-hashtable = { path = "../../common/hashtable" } diff --git a/src/query/functions-v2/tests/it/scalars/parser.rs b/src/query/functions-v2/tests/it/scalars/parser.rs index 26ffcbc5677a..c05856538e68 100644 --- a/src/query/functions-v2/tests/it/scalars/parser.rs +++ b/src/query/functions-v2/tests/it/scalars/parser.rs @@ -13,6 +13,7 @@ // limitations under the License. use common_ast::ast::BinaryOperator; +use common_ast::ast::IntervalKind; use common_ast::ast::Literal as ASTLiteral; use common_ast::ast::TypeName; use common_ast::ast::UnaryOperator; @@ -21,7 +22,6 @@ use common_ast::parser::token::Token; use common_ast::parser::tokenize_sql; use common_ast::Backtrace; use common_ast::Dialect; -use common_datavalues::IntervalKind; use common_expression::types::DataType; use common_expression::types::NumberDataType; use common_expression::Literal; diff --git a/src/query/service/src/sql/planner/semantic/type_check.rs b/src/query/service/src/sql/planner/semantic/type_check.rs index 85ad1898d3a4..c0c20c9412c9 100644 --- a/src/query/service/src/sql/planner/semantic/type_check.rs +++ b/src/query/service/src/sql/planner/semantic/type_check.rs @@ -19,6 +19,7 @@ use std::vec; use common_ast::ast::BinaryOperator; use common_ast::ast::Expr; use common_ast::ast::Identifier; +use common_ast::ast::IntervalKind as ASTIntervalKind; use common_ast::ast::Literal; use common_ast::ast::MapAccessor; use common_ast::ast::Query; @@ -1089,24 +1090,24 @@ impl<'a> TypeChecker<'a> { pub async fn resolve_extract_expr( &mut self, span: &[Token<'_>], - interval_kind: &IntervalKind, + interval_kind: &ASTIntervalKind, arg: &Expr<'_>, _required_type: Option, ) -> Result> { match interval_kind { - IntervalKind::Year => { + ASTIntervalKind::Year => { self.resolve_function(span, "to_year", &[arg], Some(TimestampType::new_impl())) .await } - IntervalKind::Quarter => { + ASTIntervalKind::Quarter => { self.resolve_function(span, "to_quarter", &[arg], Some(TimestampType::new_impl())) .await } - IntervalKind::Month => { + ASTIntervalKind::Month => { self.resolve_function(span, "to_month", &[arg], Some(TimestampType::new_impl())) .await } - IntervalKind::Day => { + ASTIntervalKind::Day => { self.resolve_function( span, "to_day_of_month", @@ -1115,19 +1116,19 @@ impl<'a> TypeChecker<'a> { ) .await } - IntervalKind::Hour => { + ASTIntervalKind::Hour => { self.resolve_function(span, "to_hour", &[arg], Some(TimestampType::new_impl())) .await } - IntervalKind::Minute => { + ASTIntervalKind::Minute => { self.resolve_function(span, "to_minute", &[arg], Some(TimestampType::new_impl())) .await } - IntervalKind::Second => { + ASTIntervalKind::Second => { self.resolve_function(span, "to_second", &[arg], Some(TimestampType::new_impl())) .await } - IntervalKind::Doy => { + ASTIntervalKind::Doy => { self.resolve_function( span, "to_day_of_year", @@ -1136,7 +1137,7 @@ impl<'a> TypeChecker<'a> { ) .await } - IntervalKind::Dow => { + ASTIntervalKind::Dow => { self.resolve_function( span, "to_day_of_week", @@ -1153,11 +1154,11 @@ impl<'a> TypeChecker<'a> { &mut self, span: &[Token<'_>], arg: &Expr<'_>, - interval_kind: &IntervalKind, + interval_kind: &ASTIntervalKind, _required_type: Option, ) -> Result> { match interval_kind { - IntervalKind::Year => { + ASTIntervalKind::Year => { self.resolve_function( span, "to_interval_year", @@ -1166,7 +1167,7 @@ impl<'a> TypeChecker<'a> { ) .await } - IntervalKind::Quarter => { + ASTIntervalKind::Quarter => { self.resolve_function( span, "to_interval_quarter", @@ -1175,7 +1176,7 @@ impl<'a> TypeChecker<'a> { ) .await } - IntervalKind::Month => { + ASTIntervalKind::Month => { self.resolve_function( span, "to_interval_month", @@ -1184,7 +1185,7 @@ impl<'a> TypeChecker<'a> { ) .await } - IntervalKind::Day => { + ASTIntervalKind::Day => { self.resolve_function( span, "to_interval_day", @@ -1193,7 +1194,7 @@ impl<'a> TypeChecker<'a> { ) .await } - IntervalKind::Hour => { + ASTIntervalKind::Hour => { self.resolve_function( span, "to_interval_hour", @@ -1202,7 +1203,7 @@ impl<'a> TypeChecker<'a> { ) .await } - IntervalKind::Minute => { + ASTIntervalKind::Minute => { self.resolve_function( span, "to_interval_minute", @@ -1211,7 +1212,7 @@ impl<'a> TypeChecker<'a> { ) .await } - IntervalKind::Second => { + ASTIntervalKind::Second => { self.resolve_function( span, "to_interval_second", @@ -1220,7 +1221,7 @@ impl<'a> TypeChecker<'a> { ) .await } - IntervalKind::Doy => { + ASTIntervalKind::Doy => { self.resolve_function( span, "to_interval_doy", @@ -1229,7 +1230,7 @@ impl<'a> TypeChecker<'a> { ) .await } - IntervalKind::Dow => { + ASTIntervalKind::Dow => { self.resolve_function( span, "to_interval_dow", @@ -1245,7 +1246,7 @@ impl<'a> TypeChecker<'a> { pub async fn resolve_date_add( &mut self, span: &[Token<'_>], - interval_kind: &IntervalKind, + interval_kind: &ASTIntervalKind, interval: &Expr<'_>, date: &Expr<'_>, _required_type: Option, @@ -1285,11 +1286,11 @@ impl<'a> TypeChecker<'a> { &mut self, span: &[Token<'_>], date: &Expr<'_>, - kind: &IntervalKind, + kind: &ASTIntervalKind, _required_type: Option, ) -> Result> { match kind { - IntervalKind::Year => { + ASTIntervalKind::Year => { self.resolve_function( span, "to_start_of_year", @@ -1298,7 +1299,7 @@ impl<'a> TypeChecker<'a> { ) .await } - IntervalKind::Quarter => { + ASTIntervalKind::Quarter => { self.resolve_function( span, "to_start_of_quarter", @@ -1307,7 +1308,7 @@ impl<'a> TypeChecker<'a> { ) .await } - IntervalKind::Month => { + ASTIntervalKind::Month => { self.resolve_function( span, "to_start_of_month", @@ -1316,7 +1317,7 @@ impl<'a> TypeChecker<'a> { ) .await } - IntervalKind::Day => { + ASTIntervalKind::Day => { self.resolve_function( span, "to_start_of_day", @@ -1325,7 +1326,7 @@ impl<'a> TypeChecker<'a> { ) .await } - IntervalKind::Hour => { + ASTIntervalKind::Hour => { self.resolve_function( span, "to_start_of_hour", @@ -1334,7 +1335,7 @@ impl<'a> TypeChecker<'a> { ) .await } - IntervalKind::Minute => { + ASTIntervalKind::Minute => { self.resolve_function( span, "to_start_of_minute", @@ -1343,7 +1344,7 @@ impl<'a> TypeChecker<'a> { ) .await } - IntervalKind::Second => { + ASTIntervalKind::Second => { self.resolve_function( span, "to_start_of_second",