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): support builtin function pi. #8509

Merged
merged 8 commits into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
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
broccoliSpicy marked this conversation as resolved.
Show resolved Hide resolved
SELECT pi(), a from f64_table
----
3.141592653589793 3.141592653589793
3.141592653589793 1.618033


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 @@ -106,6 +106,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