use crate::{
Date, SqlDecimal, finite_or_null,
operators::{eq, gt, gte, lt, lte, neq},
plus_Date_Date_LongInterval__, sign, some_existing_operator, some_function2, some_operator,
some_polymorphic_function1, some_polymorphic_function2,
timestamp::{extract_epoch_Date, extract_quarter_Date},
};
use dbsp::{algebra::F64, num_entries_scalar};
use feldera_macros::IsNone;
use feldera_types::serde_with_context::{
DeserializeWithContext, SerializeWithContext, SqlSerdeConfig,
};
use serde::{Deserialize, Serialize, de::Error as _, ser::Error as _};
use size_of::SizeOf;
use std::fmt;
use std::ops::{Add, Div, Mul, Neg, Sub};
#[cfg(doc)]
use crate::{Time, Timestamp};
#[derive(
Debug,
Default,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
SizeOf,
rkyv::Archive,
rkyv::Serialize,
Serialize,
Deserialize,
IsNone,
)]
#[archive_attr(derive(Clone, Ord, Eq, PartialEq, PartialOrd))]
#[archive(compare(PartialEq, PartialOrd))]
#[serde(transparent)]
pub struct ShortInterval {
microseconds: i64,
}
#[doc(hidden)]
pub fn reinterpret_ShortInterval(value: ShortInterval) -> i64 {
value.milliseconds()
}
some_polymorphic_function1!(reinterpret, ShortInterval, ShortInterval, i64);
#[doc(hidden)]
impl<D> ::rkyv::Deserialize<ShortInterval, D> for ArchivedShortInterval
where
D: ::rkyv::Fallible + ::core::any::Any,
{
fn deserialize(&self, deserializer: &mut D) -> Result<ShortInterval, D::Error> {
const MILLISECOND_VERSION: u32 = 4;
let version = (deserializer as &mut dyn ::core::any::Any)
.downcast_mut::<::dbsp::storage::file::Deserializer>()
.map(|deserializer| deserializer.version())
.expect("Deserializer must be of type dbsp::storage::file::Deserializer");
let value: i64 = self.microseconds.deserialize(deserializer)?;
if version <= MILLISECOND_VERSION {
Ok(ShortInterval::from_milliseconds(value))
} else {
Ok(ShortInterval::from_microseconds(value))
}
}
}
impl ShortInterval {
pub const fn from_milliseconds(milliseconds: i64) -> Self {
Self {
microseconds: milliseconds * 1000,
}
}
pub const fn from_microseconds(microseconds: i64) -> Self {
Self { microseconds }
}
pub const fn from_seconds(seconds: i64) -> Self {
Self::from_milliseconds(seconds * 1_000)
}
pub fn microseconds(&self) -> i64 {
self.microseconds
}
pub fn milliseconds(&self) -> i64 {
self.microseconds / 1000
}
pub fn nanoseconds(&self) -> i64 {
self.microseconds * 1_000
}
}
#[doc(hidden)]
pub fn abs_ShortInterval(value: ShortInterval) -> ShortInterval {
ShortInterval::from_microseconds(num::abs(value.microseconds))
}
some_polymorphic_function1!(abs, ShortInterval, ShortInterval, ShortInterval);
#[doc(hidden)]
pub fn to_bound_ShortInterval_ShortInterval_u128(value: &ShortInterval) -> u128 {
value.microseconds as u128
}
#[doc(hidden)]
pub fn to_bound_ShortInterval_Date_u128(value: &ShortInterval) -> u128 {
(value.microseconds / 1_000_000_i64 / 86400) as u128
}
#[doc(hidden)]
pub fn to_bound_ShortInterval_Date_u64(value: &ShortInterval) -> u64 {
(value.microseconds / 1_000_000_i64 / 86400) as u64
}
#[doc(hidden)]
pub fn to_bound_ShortInterval_Timestamp_u128(value: &ShortInterval) -> u128 {
value.microseconds as u128
}
#[doc(hidden)]
pub fn to_bound_ShortInterval_Timestamp_u64(value: &ShortInterval) -> u64 {
value.microseconds as u64
}
#[doc(hidden)]
pub fn to_bound_ShortInterval_Time_u128(value: &ShortInterval) -> u128 {
(value.microseconds * 1_000) as u128
}
#[doc(hidden)]
pub fn to_bound_ShortInterval_Time_u64(value: &ShortInterval) -> u64 {
(value.microseconds * 1_000) as u64
}
impl Mul<i64> for ShortInterval {
type Output = Self;
fn mul(self, rhs: i64) -> Self {
Self {
microseconds: self.microseconds * rhs,
}
}
}
impl Mul<F64> for ShortInterval {
type Output = Self;
fn mul(self, rhs: F64) -> Self {
Self {
microseconds: (F64::from(self.microseconds as f64) * rhs).into_inner() as i64,
}
}
}
impl<const P: usize, const S: usize> Mul<SqlDecimal<P, S>> for ShortInterval {
type Output = Self;
fn mul(self, rhs: SqlDecimal<P, S>) -> Self {
let us = SqlDecimal::<38, 0>::try_from(self.microseconds).unwrap_or_else(|_| {
panic!(
"overflow in short interval multiplication while converting {} to DECIMAL",
self
)
});
let mul = us
.checked_mul_generic::<P, S, 38, 0>(rhs)
.unwrap_or_else(|| panic!("overflow in INTERVAL multiplication {} * {}", self, rhs));
Self {
microseconds: mul.try_into().unwrap_or_else(|_| {
panic!("overflow in INTERVAL multiplication {} * {}", self, rhs)
}),
}
}
}
impl Div<i64> for ShortInterval {
type Output = Self;
fn div(self, rhs: i64) -> Self {
Self {
microseconds: self.microseconds / rhs,
}
}
}
impl fmt::Display for ShortInterval {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let (sign, interval) = self.into_sign_and_magnitude();
let days = extract_day_ShortInterval(interval);
let hours = extract_hour_ShortInterval(interval);
let minutes = extract_minute_ShortInterval(interval);
let seconds = extract_second_ShortInterval(interval);
let micro = interval.microseconds % 1_000_000;
if micro == 0 {
write!(
f,
"{sign}{} {:02}:{:02}:{:02}",
days, hours, minutes, seconds
)
} else {
write!(
f,
"{sign}{} {:02}:{:02}:{:02}.{:06}",
days, hours, minutes, seconds, micro
)
}
}
}
impl Div<F64> for ShortInterval {
type Output = Self;
fn div(self, rhs: F64) -> Self {
let result = div_null_ShortInterval_d(self, rhs);
match result {
None => panic!(
"overflow while dividing INTERVAL {} / {}",
self,
rhs.into_inner()
),
Some(interval) => interval,
}
}
}
impl<const P: usize, const S: usize> Div<SqlDecimal<P, S>> for ShortInterval {
type Output = Self;
fn div(self, rhs: SqlDecimal<P, S>) -> Self {
let us = SqlDecimal::<38, 0>::try_from(self.microseconds).unwrap_or_else(|_| {
panic!(
"overflow in short interval division while converting {} to DECIMAL",
self
)
});
let div = us
.checked_div_generic::<P, S, 38, 0>(rhs)
.unwrap_or_else(|| panic!("overflow in INTERVAL division {} / {}", self, rhs));
Self {
microseconds: div
.try_into()
.unwrap_or_else(|_| panic!("overflow in INTERVAL division {} / {}", self, rhs)),
}
}
}
impl Add<ShortInterval> for ShortInterval {
type Output = Self;
fn add(self, rhs: ShortInterval) -> Self {
Self {
microseconds: self
.microseconds
.checked_add(rhs.microseconds)
.unwrap_or_else(|| panic!("overflow in INTERVAL addition {} + {}", self, rhs)),
}
}
}
impl Sub<ShortInterval> for ShortInterval {
type Output = Self;
fn sub(self, rhs: ShortInterval) -> Self {
Self {
microseconds: self
.microseconds
.checked_sub(rhs.microseconds)
.unwrap_or_else(|| panic!("overflow in INTERVAL subtraction {} - {}", self, rhs)),
}
}
}
impl Neg for ShortInterval {
type Output = Self;
fn neg(self) -> Self {
Self {
microseconds: self
.microseconds
.checked_neg()
.unwrap_or_else(|| panic!("overflow in INTERVAL negation -{}", self)),
}
}
}
impl SerializeWithContext<SqlSerdeConfig> for ShortInterval {
fn serialize_with_context<S>(
&self,
_serializer: S,
_context: &SqlSerdeConfig,
) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
Err(S::Error::custom(
"serialization is not implemented for the INTERVAL type",
))
}
}
impl<'de> DeserializeWithContext<'de, SqlSerdeConfig, ()> for ShortInterval {
fn deserialize_with_context<D>(
_deserializer: D,
_context: &'de SqlSerdeConfig,
) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
Err(D::Error::custom(
"deserialization is not implemented for the INTERVAL type",
))
}
}
some_operator!(lt, ShortInterval, ShortInterval, bool);
some_operator!(gt, ShortInterval, ShortInterval, bool);
some_operator!(eq, ShortInterval, ShortInterval, bool);
some_operator!(neq, ShortInterval, ShortInterval, bool);
some_operator!(gte, ShortInterval, ShortInterval, bool);
some_operator!(lte, ShortInterval, ShortInterval, bool);
#[doc(hidden)]
pub fn div_ShortInterval_SqlDecimal<const P: usize, const S: usize>(
left: ShortInterval,
right: SqlDecimal<P, S>,
) -> ShortInterval {
left / right
}
some_polymorphic_function2!(
div <const P: usize, const S: usize>,
ShortInterval,
ShortInterval,
SqlDecimal,
SqlDecimal<P, S>,
ShortInterval
);
#[doc(hidden)]
pub fn div_null_ShortInterval_SqlDecimal<const P: usize, const S: usize>(
left: ShortInterval,
right: SqlDecimal<P, S>,
) -> Option<ShortInterval> {
let us = SqlDecimal::<38, 0>::try_from(left.microseconds()).ok()?;
let div = us.checked_div_generic::<P, S, 38, 0>(right)?;
let micros: i64 = div.try_into().ok()?;
Some(ShortInterval::from_microseconds(micros))
}
#[doc(hidden)]
pub fn div_null_ShortIntervalN_SqlDecimal<const P: usize, const S: usize>(
left: Option<ShortInterval>,
right: SqlDecimal<P, S>,
) -> Option<ShortInterval> {
let left = left?;
div_null_ShortInterval_SqlDecimal(left, right)
}
#[doc(hidden)]
pub fn div_null_ShortInterval_SqlDecimalN<const P: usize, const S: usize>(
left: ShortInterval,
right: Option<SqlDecimal<P, S>>,
) -> Option<ShortInterval> {
let right = right?;
div_null_ShortInterval_SqlDecimal(left, right)
}
#[doc(hidden)]
pub fn div_null_ShortIntervalN_SqlDecimalN<const P: usize, const S: usize>(
left: Option<ShortInterval>,
right: Option<SqlDecimal<P, S>>,
) -> Option<ShortInterval> {
let left = left?;
let right = right?;
div_null_ShortInterval_SqlDecimal(left, right)
}
#[doc(hidden)]
pub fn times_ShortInterval_i64(left: ShortInterval, right: i64) -> ShortInterval {
left * right
}
some_polymorphic_function2!(times, ShortInterval, ShortInterval, i64, i64, ShortInterval);
#[doc(hidden)]
pub fn times_ShortInterval_d(left: ShortInterval, right: F64) -> ShortInterval {
left * right
}
some_polymorphic_function2!(times, ShortInterval, ShortInterval, d, F64, ShortInterval);
#[doc(hidden)]
pub fn times_ShortInterval_SqlDecimal<const P: usize, const S: usize>(
left: ShortInterval,
right: SqlDecimal<P, S>,
) -> ShortInterval {
left * right
}
some_polymorphic_function2!(
times <const P: usize, const S: usize>,
ShortInterval,
ShortInterval,
SqlDecimal,
SqlDecimal<P, S>,
ShortInterval
);
#[doc(hidden)]
pub fn div_ShortInterval_d(left: ShortInterval, right: F64) -> ShortInterval {
left / right
}
some_polymorphic_function2!(div, ShortInterval, ShortInterval, d, F64, ShortInterval);
#[doc(hidden)]
pub fn div_null_ShortInterval_d(left: ShortInterval, right: F64) -> Option<ShortInterval> {
let div = (left.microseconds() as f64) / right.into_inner();
let fin: f64 = finite_or_null(div)?;
if fin < i64::MIN as f64 || fin > i64::MAX as f64 {
return None;
}
Some(ShortInterval::from_microseconds(fin as i64))
}
#[doc(hidden)]
pub fn div_null_ShortIntervalN_d(left: Option<ShortInterval>, right: F64) -> Option<ShortInterval> {
let left = left?;
div_null_ShortInterval_d(left, right)
}
#[doc(hidden)]
pub fn div_null_ShortInterval_dN(left: ShortInterval, right: Option<F64>) -> Option<ShortInterval> {
let right = right?;
div_null_ShortInterval_d(left, right)
}
#[doc(hidden)]
pub fn div_null_ShortIntervalN_dN(
left: Option<ShortInterval>,
right: Option<F64>,
) -> Option<ShortInterval> {
let left = left?;
let right = right?;
div_null_ShortInterval_d(left, right)
}
#[doc(hidden)]
pub fn div_ShortInterval_i64(left: ShortInterval, right: i64) -> ShortInterval {
left / right
}
some_polymorphic_function2!(div, ShortInterval, ShortInterval, i64, i64, ShortInterval);
#[doc(hidden)]
pub fn div_null_ShortInterval_i64(left: ShortInterval, right: i64) -> Option<ShortInterval> {
left.microseconds()
.checked_div(right)
.map(ShortInterval::from_microseconds)
}
#[doc(hidden)]
pub fn div_null_ShortIntervalN_i64(
left: Option<ShortInterval>,
right: i64,
) -> Option<ShortInterval> {
let left = left?;
div_null_ShortInterval_i64(left, right)
}
#[doc(hidden)]
pub fn div_null_ShortInterval_i64N(
left: ShortInterval,
right: Option<i64>,
) -> Option<ShortInterval> {
let right = right?;
div_null_ShortInterval_i64(left, right)
}
#[doc(hidden)]
pub fn div_null_ShortIntervalN_i64N(
left: Option<ShortInterval>,
right: Option<i64>,
) -> Option<ShortInterval> {
let left = left?;
let right = right?;
div_null_ShortInterval_i64(left, right)
}
#[doc(hidden)]
pub fn plus_ShortInterval_ShortInterval_ShortInterval__(
left: ShortInterval,
right: ShortInterval,
) -> ShortInterval {
left + right
}
some_function2!(
plus_ShortInterval_ShortInterval_ShortInterval,
ShortInterval,
ShortInterval,
ShortInterval
);
#[doc(hidden)]
pub fn minus_ShortInterval_ShortInterval_ShortInterval__(
left: ShortInterval,
right: ShortInterval,
) -> ShortInterval {
left - right
}
some_function2!(
minus_ShortInterval_ShortInterval_ShortInterval,
ShortInterval,
ShortInterval,
ShortInterval
);
#[derive(
Debug,
Default,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
SizeOf,
rkyv::Archive,
rkyv::Serialize,
rkyv::Deserialize,
Serialize,
Deserialize,
IsNone,
)]
#[archive_attr(derive(Clone, Ord, Eq, PartialEq, PartialOrd))]
#[archive(compare(PartialEq, PartialOrd))]
#[serde(transparent)]
pub struct LongInterval {
months: i32,
}
impl LongInterval {
pub const fn from_months(months: i32) -> Self {
Self { months }
}
pub fn months(&self) -> i32 {
self.months
}
pub fn years(&self) -> i32 {
let (mul, months) = if self.months() < 0 {
(-1, -self.months())
} else {
(1, self.months())
};
(months / 12) * mul
}
}
impl fmt::Display for LongInterval {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let months = self.months();
let sign = sign(months < 0);
let years = months.unsigned_abs() / 12;
let months = months.unsigned_abs() % 12;
write!(f, "{sign}{years}-{months:02}")
}
}
#[doc(hidden)]
pub fn reinterpret_LongInterval(value: LongInterval) -> i64 {
value.months() as i64
}
some_polymorphic_function1!(reinterpret, LongInterval, LongInterval, i64);
#[doc(hidden)]
pub fn abs_LongInterval(value: LongInterval) -> LongInterval {
LongInterval::from_months(num::abs(value.months))
}
some_polymorphic_function1!(abs, LongInterval, LongInterval, LongInterval);
impl Mul<i32> for LongInterval {
type Output = Self;
fn mul(self, rhs: i32) -> Self {
Self {
months: self.months * rhs,
}
}
}
impl Mul<F64> for LongInterval {
type Output = Self;
fn mul(self, rhs: F64) -> Self {
Self {
months: (F64::from(self.months as f64) * rhs).into_inner() as i32,
}
}
}
impl<const P: usize, const S: usize> Mul<SqlDecimal<P, S>> for LongInterval {
type Output = Self;
fn mul(self, rhs: SqlDecimal<P, S>) -> Self {
let months = SqlDecimal::<10, 0>::try_from(self.months).unwrap_or_else(|_| {
panic!(
"overflow in long interval multiplication while converting {} to DECIMAL",
self
)
});
let mul = months
.checked_mul_generic::<P, S, 10, 0>(rhs)
.unwrap_or_else(|| panic!("overflow in INTERVAL multiplication {} * {}", self, rhs));
Self {
months: mul.try_into().unwrap_or_else(|_| {
panic!("overflow in INTERVAL multiplication {} * {}", self, rhs)
}),
}
}
}
impl Div<i32> for LongInterval {
type Output = Self;
fn div(self, rhs: i32) -> Self {
Self {
months: self.months / rhs,
}
}
}
impl Div<F64> for LongInterval {
type Output = Self;
fn div(self, rhs: F64) -> Self {
let result = div_null_LongInterval_d(self, rhs);
match result {
None => panic!(
"overflow while dividing INTERVAL {} / {}",
self,
rhs.into_inner()
),
Some(interval) => interval,
}
}
}
impl<const P: usize, const S: usize> Div<SqlDecimal<P, S>> for LongInterval {
type Output = Self;
fn div(self, rhs: SqlDecimal<P, S>) -> Self {
let months = SqlDecimal::<10, 0>::try_from(self.months).unwrap_or_else(|_| {
panic!(
"overflow in short interval division while converting {} to DECIMAL",
self
)
});
let div = months
.checked_div_generic::<P, S, 10, 0>(rhs)
.unwrap_or_else(|| panic!("overflow in INTERVAL division {} / {}", self, rhs));
Self {
months: div
.try_into()
.unwrap_or_else(|_| panic!("overflow in INTERVAL division {} / {}", self, rhs)),
}
}
}
num_entries_scalar! {
ShortInterval,
LongInterval,
}
impl SerializeWithContext<SqlSerdeConfig> for LongInterval {
fn serialize_with_context<S>(
&self,
_serializer: S,
_context: &SqlSerdeConfig,
) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
Err(S::Error::custom(
"serialization is not implemented for the INTERVAL type",
))
}
}
impl<'de, AUX> DeserializeWithContext<'de, SqlSerdeConfig, AUX> for LongInterval {
fn deserialize_with_context<D>(
_deserializer: D,
_context: &'de SqlSerdeConfig,
) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
Err(D::Error::custom(
"deserialization is not implemented for the INTERVAL type",
))
}
}
impl Add<LongInterval> for LongInterval {
type Output = Self;
fn add(self, rhs: LongInterval) -> Self {
Self {
months: self
.months
.checked_add(rhs.months)
.unwrap_or_else(|| panic!("overflow in INTERVAL addition {} + {}", self, rhs)),
}
}
}
impl Sub<LongInterval> for LongInterval {
type Output = Self;
fn sub(self, rhs: LongInterval) -> Self {
Self {
months: self
.months
.checked_sub(rhs.months)
.unwrap_or_else(|| panic!("overflow in INTERVAL subtraction {} - {}", self, rhs)),
}
}
}
impl Neg for LongInterval {
type Output = Self;
fn neg(self) -> Self {
Self {
months: self
.months
.checked_neg()
.unwrap_or_else(|| panic!("overflow in INTERVAL negation -{}", self)),
}
}
}
some_operator!(lt, LongInterval, LongInterval, bool);
some_operator!(gt, LongInterval, LongInterval, bool);
some_operator!(eq, LongInterval, LongInterval, bool);
some_operator!(neq, LongInterval, LongInterval, bool);
some_operator!(gte, LongInterval, LongInterval, bool);
some_operator!(lte, LongInterval, LongInterval, bool);
#[doc(hidden)]
pub fn times_LongInterval_i32(left: LongInterval, right: i32) -> LongInterval {
left * right
}
some_polymorphic_function2!(times, LongInterval, LongInterval, i32, i32, LongInterval);
#[doc(hidden)]
pub fn times_LongInterval_d(left: LongInterval, right: F64) -> LongInterval {
left * right
}
some_polymorphic_function2!(times, LongInterval, LongInterval, d, F64, LongInterval);
#[doc(hidden)]
pub fn times_LongInterval_SqlDecimal<const P: usize, const S: usize>(
left: LongInterval,
right: SqlDecimal<P, S>,
) -> LongInterval {
left * right
}
some_polymorphic_function2!(
times <const P: usize, const S: usize>,
LongInterval,
LongInterval,
SqlDecimal,
SqlDecimal<P, S>,
LongInterval
);
#[doc(hidden)]
pub fn div_LongInterval_i32(left: LongInterval, right: i32) -> LongInterval {
left / right
}
some_polymorphic_function2!(div, LongInterval, LongInterval, i32, i32, LongInterval);
#[doc(hidden)]
pub fn div_null_LongInterval_i32(left: LongInterval, right: i32) -> Option<LongInterval> {
left.months
.checked_div(right)
.map(LongInterval::from_months)
}
#[doc(hidden)]
pub fn div_null_LongIntervalN_i32(left: Option<LongInterval>, right: i32) -> Option<LongInterval> {
let left = left?;
div_null_LongInterval_i32(left, right)
}
#[doc(hidden)]
pub fn div_null_LongInterval_i32N(left: LongInterval, right: Option<i32>) -> Option<LongInterval> {
let right = right?;
div_null_LongInterval_i32(left, right)
}
#[doc(hidden)]
pub fn div_null_LongIntervalN_i32N(
left: Option<LongInterval>,
right: Option<i32>,
) -> Option<LongInterval> {
let left = left?;
let right = right?;
div_null_LongInterval_i32(left, right)
}
#[doc(hidden)]
pub fn div_LongInterval_d(left: LongInterval, right: F64) -> LongInterval {
left / right
}
some_polymorphic_function2!(div, LongInterval, LongInterval, d, F64, LongInterval);
#[doc(hidden)]
pub fn div_null_LongInterval_d(left: LongInterval, right: F64) -> Option<LongInterval> {
let div: f64 = (left.months() as f64) / right.into_inner();
let fin: f64 = finite_or_null(div)?;
if fin < i32::MIN as f64 || fin > i32::MAX as f64 {
return None;
}
Some(LongInterval::from_months(fin as i32))
}
#[doc(hidden)]
pub fn div_null_LongIntervalN_d(left: Option<LongInterval>, right: F64) -> Option<LongInterval> {
let left = left?;
div_null_LongInterval_d(left, right)
}
#[doc(hidden)]
pub fn div_null_LongInterval_dN(left: LongInterval, right: Option<F64>) -> Option<LongInterval> {
let right = right?;
div_null_LongInterval_d(left, right)
}
#[doc(hidden)]
pub fn div_null_LongIntervalN_dN(
left: Option<LongInterval>,
right: Option<F64>,
) -> Option<LongInterval> {
let left = left?;
let right = right?;
div_null_LongInterval_d(left, right)
}
#[doc(hidden)]
pub fn div_LongInterval_SqlDecimal<const P: usize, const S: usize>(
left: LongInterval,
right: SqlDecimal<P, S>,
) -> LongInterval {
left / right
}
some_polymorphic_function2!(
div <const P: usize, const S: usize>,
LongInterval,
LongInterval,
SqlDecimal,
SqlDecimal<P, S>,
LongInterval
);
#[doc(hidden)]
pub fn div_null_LongInterval_SqlDecimal<const P: usize, const S: usize>(
left: LongInterval,
right: SqlDecimal<P, S>,
) -> Option<LongInterval> {
let m = SqlDecimal::<38, 0>::try_from(left.months()).ok()?;
let div = m.checked_div_generic::<P, S, 38, 0>(right)?;
let months: i32 = div.try_into().ok()?;
Some(LongInterval::from_months(months))
}
#[doc(hidden)]
pub fn div_null_LongIntervalN_SqlDecimal<const P: usize, const S: usize>(
left: Option<LongInterval>,
right: SqlDecimal<P, S>,
) -> Option<LongInterval> {
let left = left?;
div_null_LongInterval_SqlDecimal(left, right)
}
#[doc(hidden)]
pub fn div_null_LongInterval_SqlDecimalN<const P: usize, const S: usize>(
left: LongInterval,
right: Option<SqlDecimal<P, S>>,
) -> Option<LongInterval> {
let right = right?;
div_null_LongInterval_SqlDecimal(left, right)
}
#[doc(hidden)]
pub fn div_null_LongIntervalN_SqlDecimalN<const P: usize, const S: usize>(
left: Option<LongInterval>,
right: Option<SqlDecimal<P, S>>,
) -> Option<LongInterval> {
let left = left?;
let right = right?;
div_null_LongInterval_SqlDecimal(left, right)
}
#[doc(hidden)]
pub fn plus_LongInterval_LongInterval_LongInterval__(
left: LongInterval,
right: LongInterval,
) -> LongInterval {
left + right
}
some_function2!(
plus_LongInterval_LongInterval_LongInterval,
LongInterval,
LongInterval,
LongInterval
);
#[doc(hidden)]
pub fn minus_LongInterval_LongInterval_LongInterval__(
left: LongInterval,
right: LongInterval,
) -> LongInterval {
left - right
}
some_function2!(
minus_LongInterval_LongInterval_LongInterval,
LongInterval,
LongInterval,
LongInterval
);
#[doc(hidden)]
pub fn extract_year_LongInterval(value: LongInterval) -> i64 {
(value.months / 12).into()
}
#[doc(hidden)]
pub fn extract_month_LongInterval(value: LongInterval) -> i64 {
(value.months % 12).into()
}
#[doc(hidden)]
pub fn extract_quarter_LongInterval(value: LongInterval) -> i64 {
let dt = Date::from_days(0);
let dt = plus_Date_Date_LongInterval__(dt, value);
extract_quarter_Date(dt)
}
#[doc(hidden)]
pub fn extract_decade_LongInterval(value: LongInterval) -> i64 {
let year = extract_year_LongInterval(value);
year / 10
}
#[doc(hidden)]
pub fn extract_millennium_LongInterval(value: LongInterval) -> i64 {
let year = extract_year_LongInterval(value);
year / 1000 }
#[doc(hidden)]
pub fn extract_century_LongInterval(value: LongInterval) -> i64 {
let year = extract_year_LongInterval(value);
year / 100 }
#[doc(hidden)]
pub fn extract_day_LongInterval(_value: LongInterval) -> i64 {
0
}
#[doc(hidden)]
pub fn extract_epoch_LongInterval(value: LongInterval) -> i64 {
let dt = Date::from_days(0);
let dt = plus_Date_Date_LongInterval__(dt, value);
extract_epoch_Date(dt)
}
#[doc(hidden)]
pub fn extract_millisecond_LongInterval(_value: LongInterval) -> i64 {
0
}
#[doc(hidden)]
pub fn extract_microsecond_LongInterval(_value: LongInterval) -> i64 {
0
}
#[doc(hidden)]
pub fn extract_second_LongInterval(_value: LongInterval) -> i64 {
0
}
#[doc(hidden)]
pub fn extract_minute_LongInterval(_value: LongInterval) -> i64 {
0
}
#[doc(hidden)]
pub fn extract_hour_LongInterval(_value: LongInterval) -> i64 {
0
}
#[doc(hidden)]
pub fn short_interval_to_integer(value: ShortInterval) -> i64 {
value.microseconds
}
#[doc(hidden)]
pub fn integer_to_short_interval(value: i64) -> ShortInterval {
ShortInterval::from_microseconds(value)
}
#[doc(hidden)]
pub fn extract_day_ShortInterval(value: ShortInterval) -> i64 {
value.microseconds() / 86_400_000_000_i64
}
#[doc(hidden)]
pub fn extract_epoch_ShortInterval(value: ShortInterval) -> i64 {
value.microseconds() / 1_000_000
}
#[doc(hidden)]
pub fn extract_millisecond_ShortInterval(value: ShortInterval) -> i64 {
(value.microseconds() % 60_000_000_i64) / 1000
}
#[doc(hidden)]
pub fn extract_microsecond_ShortInterval(value: ShortInterval) -> i64 {
value.microseconds() % 60_000_000_i64
}
#[doc(hidden)]
pub fn extract_second_ShortInterval(value: ShortInterval) -> i64 {
(value.microseconds() / 1_000_000) % 60
}
#[doc(hidden)]
pub fn extract_minute_ShortInterval(value: ShortInterval) -> i64 {
(value.microseconds() / 60_000_000_i64) % 60
}
#[doc(hidden)]
pub fn extract_hour_ShortInterval(value: ShortInterval) -> i64 {
(value.microseconds() / (60 * 60 * 1000 * 1000i64)) % 24
}
some_polymorphic_function1!(extract_year, LongInterval, LongInterval, i64);
some_polymorphic_function1!(extract_month, LongInterval, LongInterval, i64);
some_polymorphic_function1!(extract_quarter, LongInterval, LongInterval, i64);
some_polymorphic_function1!(extract_decade, LongInterval, LongInterval, i64);
some_polymorphic_function1!(extract_century, LongInterval, LongInterval, i64);
some_polymorphic_function1!(extract_millennium, LongInterval, LongInterval, i64);
some_polymorphic_function1!(extract_day, LongInterval, LongInterval, i64);
some_polymorphic_function1!(extract_epoch, LongInterval, LongInterval, i64);
some_polymorphic_function1!(extract_millisecond, LongInterval, LongInterval, i64);
some_polymorphic_function1!(extract_microsecond, LongInterval, LongInterval, i64);
some_polymorphic_function1!(extract_second, LongInterval, LongInterval, i64);
some_polymorphic_function1!(extract_minute, LongInterval, LongInterval, i64);
some_polymorphic_function1!(extract_hour, LongInterval, LongInterval, i64);
some_polymorphic_function1!(extract_day, ShortInterval, ShortInterval, i64);
some_polymorphic_function1!(extract_epoch, ShortInterval, ShortInterval, i64);
some_polymorphic_function1!(extract_millisecond, ShortInterval, ShortInterval, i64);
some_polymorphic_function1!(extract_microsecond, ShortInterval, ShortInterval, i64);
some_polymorphic_function1!(extract_second, ShortInterval, ShortInterval, i64);
some_polymorphic_function1!(extract_minute, ShortInterval, ShortInterval, i64);
some_polymorphic_function1!(extract_hour, ShortInterval, ShortInterval, i64);