Skip to content

Commit

Permalink
feat(expr): support builtin function pi. (risingwavelabs#8509)
Browse files Browse the repository at this point in the history
  • Loading branch information
broccoliSpicy authored Mar 15, 2023
1 parent 8be4734 commit 9f68cef
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 0 deletions.
7 changes: 7 additions & 0 deletions dashboard/proto/gen/expr.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions e2e_test/batch/functions/pi.slt.part
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
statement ok
create table f64_table (a double);

statement ok
insert into f64_table values(pi());

statement ok
insert into f64_table values(1.618033);

statement ok
create table f32_table (a real);

statement ok
insert into f32_table values(pi());

query I
SELECT pi()
----
3.141592653589793

query I rowsort
SELECT pi(), a from f64_table
----
3.141592653589793 1.618033
3.141592653589793 3.141592653589793


statement ok
drop table f64_table

statement ok
drop table f32_table
3 changes: 3 additions & 0 deletions proto/expr.proto
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ message ExprNode {
JSONB_TYPEOF = 602;
JSONB_ARRAY_LENGTH = 603;

// Functions that return a constant value
PI = 610;

// Non-pure functions below (> 1000)
// ------------------------
// Internal functions
Expand Down
5 changes: 5 additions & 0 deletions src/frontend/src/binder/expr/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,9 @@ impl Binder {
raw_call(ExprType::Now)(binder, inputs)
})
}
fn pi() -> Handle {
raw_literal(ExprImpl::literal_f64(std::f64::consts::PI))
}

static HANDLES: LazyLock<HashMap<&'static str, Handle>> = LazyLock::new(|| {
[
Expand Down Expand Up @@ -383,6 +386,8 @@ impl Binder {
("jsonb_array_element_text", raw_call(ExprType::JsonbAccessStr)),
("jsonb_typeof", raw_call(ExprType::JsonbTypeof)),
("jsonb_array_length", raw_call(ExprType::JsonbArrayLength)),
// Functions that return a constant value
("pi", pi()),
// System information operations.
(
"pg_typeof",
Expand Down
6 changes: 6 additions & 0 deletions src/frontend/src/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ impl ExprImpl {
Literal::new(Some(v.to_scalar_value()), DataType::Int32).into()
}

/// A literal float64 value.
#[inline(always)]
pub fn literal_f64(v: f64) -> Self {
Literal::new(Some(v.into()), DataType::Float64).into()
}

/// A literal boolean value.
#[inline(always)]
pub fn literal_bool(v: bool) -> Self {
Expand Down

0 comments on commit 9f68cef

Please sign in to comment.