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

Implement the sign function #8900

Closed
Tracked by #8806
neverchanje opened this issue Mar 31, 2023 · 9 comments · Fixed by #10819
Closed
Tracked by #8806

Implement the sign function #8900

neverchanje opened this issue Mar 31, 2023 · 9 comments · Fixed by #10819
Assignees
Labels
component/func-expr Support a SQL function or operator
Milestone

Comments

@neverchanje
Copy link
Contributor

neverchanje commented Mar 31, 2023

Purpose

The sign() function returns the sign of the specified argument.

Parameters

The sign() function takes one parameter:

dp or numeric: The argument to compute the sign for.

Return Value

The sign() function returns an integer that represents the sign of the specified argument, according to the following rules:

  • If the argument is positive, 1 is returned.
  • If the argument is negative, -1 is returned.
  • If the argument is zero, 0 is returned.

Example

SELECT sign(5); -- Returns 1
SELECT sign(-5); -- Returns -1
SELECT sign(0); -- Returns 0

Regress test to enable

-- sign
--@ select sign(f1) as sign_f1 from float8_tbl f;

select sign(f1) as sign_f1 from float8_tbl f;
sign_f1
---------
0
1
-1
1
1
(5 rows)

@github-actions github-actions bot added this to the release-0.19 milestone Mar 31, 2023
@xiangjinwu xiangjinwu added the component/func-expr Support a SQL function or operator label Apr 4, 2023
@broccoliSpicy
Copy link
Contributor

there are actually some bit hacks we could utilize for this function.
for example:
sign = (v > 0) - (v < 0);

bit_hack

do we care about performance that much here?
to implement these hacks we might need to introduce C to risingwave, seems too trivial a reason to mess up the current compilation/build
what about assembly, what if doing so limits risingwave's supporting architecture

@xxchan
Copy link
Member

xxchan commented Apr 14, 2023

Hmmm, why can't this hack be done in Rust? 👀

@xxchan
Copy link
Member

xxchan commented Apr 14, 2023

And actually, there's i32::signum https://doc.rust-lang.org/std/primitive.i32.html#method.signum

@broccoliSpicy
Copy link
Contributor

ha, my bad, I thought casting from boolean to integer is disallowed in Rust

// Type your code here, or load an example.
pub fn sign(num: i32) -> i32 {
    ((num > 0) as i32) - ((num < 0) as i32)
}

// If you use `main()`, declare it as `pub` to see it in the output:
// pub fn main() { ... }
example::sign:
        mov     ecx, edi
        sar     ecx, 31
        xor     eax, eax
        test    edi, edi
        setne   al
        or      eax, ecx
        ret

rustc's optimization is doing quite a good job here(all 'cause of llvm?)
Rust is cool here

this result is produced by this tool rustc-nightly
-C opt-level=3 -C target-cpu=cannonlake

@xxchan
Copy link
Member

xxchan commented Apr 15, 2023

Hmmm, is this bit hack better? 👀 You might find a chance to contribute to Rust std! 😄

image

@xxchan
Copy link
Member

xxchan commented Apr 15, 2023

Oh, things become different in nightly... 🤔

image

@broccoliSpicy
Copy link
Contributor

haha:smile:, we might be a few months late.

I am sure there will be plenty of other chances for us

this signum function's implementation is lit, I think we should use it

@broccoliSpicy
Copy link
Contributor

broccoliSpicy commented Apr 16, 2023

reference:
Rust PR for signum optimizition

@neverchanje
Copy link
Contributor Author

neverchanje commented May 9, 2023

Hi @xxchan Can I assign you as the owner for this issue? Please feel free to uncheck the assignee if you don't have time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component/func-expr Support a SQL function or operator
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants