Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(expr): Various trigonometric functions #8838

Merged
merged 23 commits into from
Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions proto/expr.proto
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ message ExprNode {
STRPOS = 241;
TO_ASCII = 242;
TO_HEX = 243;
SIN = 245;
COS = 246;
TAN = 247;
COT = 248;
ASIN = 249;
ACOS = 250;
ATAN = 251;
ATAN2 = 252;

// Boolean comparison
IS_TRUE = 301;
Expand Down
2 changes: 1 addition & 1 deletion src/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ rand = "0.8"
regex = "1"
reqwest = { version = "0.11", features = ["json"] }
risingwave_pb = { path = "../prost" }
rust_decimal = { version = "1", features = ["db-tokio-postgres"] }
rust_decimal = { version = "1", features = ["db-tokio-postgres", "maths"] }
CAJan93 marked this conversation as resolved.
Show resolved Hide resolved
ryu = "1.0"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
Expand Down
66 changes: 66 additions & 0 deletions src/common/src/types/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use std::ops::{Add, Div, Mul, Neg, Rem, Sub};
use bytes::{BufMut, Bytes, BytesMut};
use num_traits::{CheckedAdd, CheckedDiv, CheckedMul, CheckedNeg, CheckedRem, CheckedSub, Zero};
use postgres_types::{ToSql, Type};
use rust_decimal::prelude::MathematicalOps;
pub use rust_decimal::prelude::{FromPrimitive, FromStr, ToPrimitive};
use rust_decimal::{Decimal as RustDecimal, Error, RoundingStrategy};

Expand Down Expand Up @@ -499,6 +500,71 @@ impl Decimal {
}
}

#[must_use]
pub fn sin(&self) -> Self {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI In PostgreSQL, trigonometric functions are only defined for double precision. Inputs of other types are implicitly casted to double precision, and the output is always double precision.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for pointing that out :)

match self {
Self::Normalized(d) => Self::Normalized(d.sin()),
d => *d,
}
}

#[must_use]
pub fn cos(&self) -> Self {
match self {
Self::Normalized(d) => Self::Normalized(d.cos()),
d => *d,
}
}

#[must_use]
pub fn tan(&self) -> Self {
match self {
Self::Normalized(d) => Self::Normalized(d.tan()),
d => *d,
}
}

#[must_use]
pub fn cot(&self) -> Self {
match self {
Self::Normalized(d) => Self::Normalized(d.cos() / d.sin()),
d => *d,
}
}

#[must_use]
pub fn asin(&self) -> Self {
match self {
Self::Normalized(d) => Self::Normalized(*d).to_f64().expect("Expected valid f64 value").asin().into(),
d => d.to_f64().expect("Expected valid f64 value").asin().into(),
}
}

#[must_use]
pub fn acos(&self) -> Self {
match self {
Self::Normalized(d) => Self::Normalized(*d).to_f64().expect("Expected valid f64 value").acos().into(),
d => d.to_f64().expect("Expected valid f64 value").acos().into(),
}
}

#[must_use]
pub fn atan(&self) -> Self {
match self {
Self::Normalized(d) => Self::Normalized(*d).to_f64().expect("Expected valid f64 value").atan().into(),
d => d.to_f64().expect("Expected valid f64 value").atan().into(),
}
}

#[must_use]
pub fn atan2(&self) -> Self {
match self {
// Somehow pass 2 args here. Do not just pass 0
Self::Normalized(d) => Self::Normalized(*d).to_f64().expect("Expected valid f64 value").atan2(0 as f64).into(),
d => d.to_f64().expect("Expected valid f64 value").atan2(0 as f64).into(),
}
}

#[must_use]
pub fn floor(&self) -> Self {
match self {
Expand Down
84 changes: 84 additions & 0 deletions src/expr/src/vector_op/round.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,90 @@ pub fn round_digits<D: Into<i32>>(input: Decimal, digits: D) -> Decimal {
}
}

// TODO: add sin here?

#[function("sin(float64) -> float64")]
pub fn sin_f64(input: F64) -> F64 {
f64::sin(input.0).into()
}

#[function("sin(decimal) -> decimal")]
pub fn sin_decimal(input: Decimal) -> Decimal {
input.sin()
}

#[function("cos(float64) -> float64")]
pub fn cos_f64(input: F64) -> F64 {
f64::cos(input.0).into()
}

#[function("cos(decimal) -> decimal")]
pub fn cos_decimal(input: Decimal) -> Decimal {
input.cos()
}

#[function("tan(float64) -> float64")]
pub fn tan_f64(input: F64) -> F64 {
f64::tan(input.0).into()
}

#[function("tan(decimal) -> decimal")]
pub fn tan_decimal(input: Decimal) -> Decimal {
input.tan()
}

#[function("cot(float64) -> float64")]
pub fn cot_f64(input: F64) -> F64 {
f64::cot(input.0).into() // TODO
}

#[function("cot(decimal) -> decimal")]
pub fn cot_decimal(input: Decimal) -> Decimal {
input.cot()
}

#[function("asin(float64) -> float64")]
pub fn asin_f64(input: F64) -> F64 {
f64::asin(input.0).into()
}

#[function("asin(decimal) -> decimal")]
pub fn asin_decimal(input: Decimal) -> Decimal {
input.asin()
}

#[function("acos(float64) -> float64")]
pub fn acos_f64(input: F64) -> F64 {
f64::acos(input.0).into()
}

#[function("acos(decimal) -> decimal")]
pub fn acos_decimal(input: Decimal) -> Decimal {
input.acos()
}

#[function("atan(float64) -> float64")]
pub fn atan_f64(input: F64) -> F64 {
f64::atan(input.0).into()
}

#[function("atan(decimal) -> decimal")]
pub fn atan_decimal(input: Decimal) -> Decimal {
input.atan()
}
// TODO: how do I pass 2 args here?
#[function("atan2(float64, float64) -> float64")]
pub fn atan2_f64(input: F64) -> F64 {
f64::atan2(input.0, input.1).into()
}


#[function("atan2(decimal) -> decimal")]
pub fn atan2_decimal(input: Decimal) -> Decimal {
input.atan2()
}


#[function("ceil(float64) -> float64")]
pub fn ceil_f64(input: F64) -> F64 {
f64::ceil(input.0).into()
Expand Down
10 changes: 10 additions & 0 deletions src/frontend/src/binder/expr/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,16 @@ impl Binder {
("abs", raw_call(ExprType::Abs)),
("exp", raw_call(ExprType::Exp)),
("mod", raw_call(ExprType::Modulus)),
("sin", raw_call(ExprType::Sin)),
("cos", raw_call(ExprType::Cos)),
("tan", raw_call(ExprType::Tan)),
("cot", raw_call(ExprType::Cot)),
("asin", raw_call(ExprType::Asin)),
("acos", raw_call(ExprType::Acos)),
("atan", raw_call(ExprType::Atan)),
("atan2", raw_call(ExprType::Atan2)),

// TODO: add here
(
"to_timestamp",
dispatch_by_len(vec![
Expand Down