Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Support mathematical functions: conv, crc32, mod, pow/power, round, sign, sqrt, truncate #577

Merged
merged 14 commits into from
Jul 20, 2020

Conversation

chloe-zh
Copy link
Member

@chloe-zh chloe-zh commented Jul 15, 2020

Issue #, if available:

Description of changes:

  • conv
    usage: conv(x, a, b) converts the number x from a base to b base
    argument type: x: string, a: integer, b: integer
    return type: string

  • crc32
    usage: calculates a cyclic redundancy check value and returns a 32-bit unsigned value
    argument type: string
    return type: long

  • mod
    usage: mod(n, m) calculates the remainder of the number n divided by m
    argument type: integer/long/float/double
    return type: widen type between types of n and m

  • pow/power
    usage: pow(x, y) calculates the value of x raised to the power of y
    argument type: integer/long/float/double
    return type: double

  • round
    usage: round(x, d) rounds the argument x to d decimal places, d defaults to 0 if not specified
    argument type: integer/long/float/double
    return type map:
    (integer/long [,integer]) -> long
    (float/double [,integer]) -> double

  • sign
    usage: returns the sign of the argument as -1, 0, or 1, depending on whether the number is negative, zero, or positive
    argument type: integer/long/float/double
    return type: integer

  • sqrt
    usage: calculate the square root of a nonnegative number
    argument type: integer/long/float/double
    return type map:
    (nonnegative) integer/long/float/double -> double
    (negative) integer/long/float/double -> null

  • truncate
    usage: truncate(x, d) returns the number x, truncated to d decimal places
    argument type: integer/long/float/double
    return type map:
    integer/long -> long
    float/double -> double

Reference

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@dai-chen
Copy link
Member

Could you check if we can avoid and fix this issue in new engine? #331

@chloe-zh chloe-zh changed the title Support mathematical functions: conv, crc32, mod, pow/power, sign, sqrt, truncate Support mathematical functions: conv, crc32, mod, pow/power, round, sign, sqrt, truncate Jul 16, 2020
@chloe-zh chloe-zh marked this pull request as ready for review July 16, 2020 04:26
@dai-chen
Copy link
Member

I think the description of this PR should be added to our doc. Could you add it and make this a doctest file: https://github.com/opendistro-for-elasticsearch/sql/blob/master/docs/user/dql/functions.rst

@chloe-zh
Copy link
Member Author

Could you check if we can avoid and fix this issue in new engine? #331

MySQL returns null for functions with illegal arguments, like ln(-1), log(-1) etc, and for the result of infinity, and we keep consistent with this behavior. But for special cases like pow(0, 0) (Java behavior is returning 1), I didn't find any reference in MySQL manual, so for now it still follows the java behaviors.

@dai-chen
Copy link
Member

Could you check if we can avoid and fix this issue in new engine? #331

MySQL returns null for functions with illegal arguments, like ln(-1), log(-1) etc, and for the result of infinity, and we keep consistent with this behavior. But for special cases like pow(0, 0) (Java behavior is returning 1), I didn't find any reference in MySQL manual, so for now it still follows the java behaviors.

Sounds good! I just tested pow(0,0) in MySQL. It returns 1 same as Java. :)

@chloe-zh chloe-zh linked an issue Jul 16, 2020 that may be closed by this pull request
Copy link
Member

@dai-chen dai-chen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks for the changes!

@chloe-zh chloe-zh self-assigned this Jul 17, 2020
@chloe-zh chloe-zh merged commit 9d72867 into opendistro-for-elasticsearch:develop Jul 20, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

JSON does not allow non-finite numbers
3 participants