Skip to content

scalar function round ignores second parameter #4191

Closed

Description

@andygrove

Describe the bug

round accepts two arguments, but is also defined as a unary function. The second parameter is ignored.

❯ select round(3.141592, 0);
+-----------------------------------+
| round(Float64(3.141592),Int64(0)) |
+-----------------------------------+
| 3                                 |
+-----------------------------------+
1 row in set. Query took 0.000 seconds.

❯ select round(3.141592, -2);
+------------------------------------+
| round(Float64(3.141592),Int64(-2)) |
+------------------------------------+
| 3                                  |
+------------------------------------+
1 row in set. Query took 0.000 seconds.

❯ select round(3.141592, 2);
+-----------------------------------+
| round(Float64(3.141592),Int64(2)) |
+-----------------------------------+
| 3                                 |
+-----------------------------------+
1 row in set. Query took 0.000 seconds.

To Reproduce
See above

Expected behavior
I expect the behavior to be consistent with Postgres round(v numeric, s int). User guide will also need updating.

Additional context
Related to #2420

Activity

  1. added this to the 15.0.0 milestone on Nov 12, 2022
  2. HaoYang670 commented on Nov 15, 2022

    @HaoYang670
    Contributor

    Not sure, but I wonder if we could support this in logical phase.
    Given the following 2 lemmas:

    1. forall source: FloatArray, round(source) = round(source, 0)
    2. forall source: FloatArray n: IntArray | IntScalar,
            round(source, n) = div(
                                  round(mul(
                                          source, 
                                          pow(10, n))), 
                                  pow(10, n))

    we can unfold the round function in the logical plan stage and don't need to modify the physical stage.

    One concern here is that Decimal type might be better than Float type because we've found some corner cases in floating point multiplication and division: #4072

  3. DDtKey commented on Jan 22, 2023

    @DDtKey
    Contributor

    Just wondering if anyone is working on it? It's relevant for current version. I see the same behaviour

  4. HaoYang670 commented on Jan 22, 2023

    @HaoYang670
    Contributor

    I've pushed a draft PR (#4234) to unfold the round function with 2 args in logical plan.
    But it seems like not a good solution because it would reduce the extensibility of the logical optimizer.

    Based on Andy's comment (#4234 (comment)) we should either support this or return an error in the physical plan.

    Just wondering if anyone is working on it?

    No, I guess.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions