Skip to content

Commit 8210e2a

Browse files
committed
feat: enable the nightly feature for gamma/ln_gamma function
Signed-off-by: Alan Tang <[email protected]>
1 parent 506486c commit 8210e2a

File tree

5 files changed

+28
-53
lines changed

5 files changed

+28
-53
lines changed

Cargo.lock

Lines changed: 0 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/common/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#![feature(register_tool)]
3636
#![feature(btree_cursors)]
3737
#![feature(assert_matches)]
38+
#![feature(float_gamma)]
3839
#![feature(anonymous_lifetime_in_impl_trait)]
3940
#![feature(vec_into_raw_parts)]
4041
#![feature(exact_div)]

src/common/src/types/ordered_float.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,17 @@ impl<T: One> One for OrderedFloat<T> {
578578
}
579579
}
580580

581+
impl OrderedFloat<f64> {
582+
pub fn gamma(self) -> Self {
583+
OrderedFloat(self.0.gamma())
584+
}
585+
586+
pub fn ln_gamma(self) -> (Self, i32) {
587+
let (result, sign) = self.0.ln_gamma();
588+
(OrderedFloat(result), sign)
589+
}
590+
}
591+
581592
/// Similar to [`num_traits::Float`], but without requiring `NumCast` and `ToPrimitive`.
582593
#[easy_ext::ext(FloatExt)]
583594
pub impl<T: Float> OrderedFloat<T>

src/expr/impl/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ serde_json = "1"
5959
sha1 = "0.10"
6060
sha2 = "0.10"
6161
smallvec = { workspace = true }
62-
special = "0.11.4"
6362
sql-json-path = { version = "0.1.1", features = ["jsonbb"] }
6463
thiserror = { workspace = true }
6564
thiserror-ext = { workspace = true }

src/expr/impl/src/scalar/arithmetic_op.rs

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use risingwave_common::types::{
2121
};
2222
use risingwave_expr::{ExprError, Result, function};
2323
use rust_decimal::MathematicalOps;
24-
use special::{Gamma, Primitive};
2524

2625
#[function("add(*int, *int) -> auto")]
2726
#[function("add(decimal, decimal) -> auto")]
@@ -448,13 +447,13 @@ pub fn decimal_trim_scale(d: Decimal) -> Decimal {
448447
/// ----
449448
/// NaN
450449
///
451-
/// statement error
450+
/// statement error overflow
452451
/// SELECT gamma('-inf'::float8);
453452
///
454453
/// query R
455454
/// SELECT gamma('0.5'::float8);
456455
/// ----
457-
/// 1.7724538509055159
456+
/// 1.772453850905516
458457
///
459458
/// query R
460459
/// SELECT gamma('1'::float8);
@@ -481,16 +480,16 @@ pub fn decimal_trim_scale(d: Decimal) -> Decimal {
481480
/// ----
482481
/// 24
483482
///
484-
/// statement error
483+
/// statement error overflow
485484
/// SELECT gamma('-1'::float8);
486485
///
487-
/// statement error
486+
/// statement error underflow
488487
/// SELECT gamma('-1000.5'::float8);
489488
///
490-
/// statement error
489+
/// statement error overflow
491490
/// SELECT gamma('0'::float8);
492491
///
493-
/// statement error
492+
/// statement error overflow
494493
/// SELECT gamma('1000'::float8);
495494
/// ```
496495
#[function("gamma(float8) -> float8")]
@@ -500,30 +499,18 @@ pub fn gamma_f64(input: F64) -> Result<F64> {
500499
return Ok(result);
501500
} else if input.is_infinite() {
502501
if input.is_negative() {
503-
return Err(ExprError::InvalidParam {
504-
name: "gamma",
505-
reason: "value out of range: overflow".into(),
506-
});
502+
return Err(ExprError::NumericOverflow);
507503
}
508504
} else {
509-
result = F64::from(Gamma::gamma(input.0));
505+
result = input.gamma();
510506
if result.is_nan() || result.is_infinite() {
511507
if !result.is_zero() {
512-
return Err(ExprError::InvalidParam {
513-
name: "gamma",
514-
reason: "value out of range: overflow".into(),
515-
});
508+
return Err(ExprError::NumericOverflow);
516509
} else {
517-
return Err(ExprError::InvalidParam {
518-
name: "gamma",
519-
reason: "value out of range: underflow".into(),
520-
});
510+
return Err(ExprError::NumericUnderflow);
521511
}
522512
} else if result.is_zero() {
523-
return Err(ExprError::InvalidParam {
524-
name: "gamma",
525-
reason: "value out of range: underflow".into(),
526-
});
513+
return Err(ExprError::NumericUnderflow);
527514
}
528515
}
529516
Ok(result)
@@ -575,33 +562,30 @@ pub fn gamma_f64(input: F64) -> Result<F64> {
575562
/// ----
576563
/// 3.1780538303479458
577564
///
578-
/// statement error
565+
/// statement error overflow
579566
/// SELECT lgamma('-1'::float8);
580567
///
581568
/// query R
582569
/// SELECT lgamma('-1000.5'::float8);
583570
/// ----
584571
/// -5914.437701116853
585572
///
586-
/// statement error
573+
/// statement error overflow
587574
/// SELECT gamma('0'::float8);
588575
///
589576
/// query R
590577
/// SELECT lgamma('1000'::float8);
591578
/// ----
592579
/// 5905.220423209181
593580
///
594-
/// statement error
581+
/// statement error overflow
595582
/// SELECT gamma('1e308'::float8);
596583
/// ```
597584
#[function("lgamma(float8) -> float8")]
598585
pub fn lgamma_f64(input: F64) -> Result<F64> {
599-
let (result, _sign) = input.0.lgamma();
586+
let (result, _sign) = input.ln_gamma();
600587
if result.is_infinite() && input.is_finite() {
601-
return Err(ExprError::InvalidParam {
602-
name: "lgamma",
603-
reason: "value out of range: overflow".into(),
604-
});
588+
return Err(ExprError::NumericOverflow);
605589
}
606590
Ok(F64::from(result))
607591
}

0 commit comments

Comments
 (0)