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

[function] refactor some arithmetic functions #1992

Merged
merged 1 commit into from
Feb 8, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -22,36 +22,60 @@ specific language governing permissions and limitations
under the License.
-->

## bin
## Description

### description
#### Syntax
Converts decimal numbers to binary text.

`STRING bin(BIGINT x)`
Convert the decimal number `x` to binary.
## Syntax

### example
```sql
BIN(<a>)
```

## Parameters

| Parameter | Description |
| -- | -- |
| `<a>` | Decimal value to be converted |

## Return Value

The binary representation of the parameter `<a>`. When `<a>` is negative, the result is its 64-bit complement.

## Examples

```sql
select bin(0);
```
mysql> select bin(0);

```text
+--------+
| bin(0) |
+--------+
| 0 |
+--------+
mysql> select bin(10);
+---------+
| bin(10) |
+---------+
| 1010 |
+---------+
mysql> select bin(-3);
```

```sql
select bin(-1);
```

```text
+------------------------------------------------------------------+
| bin(-3) |
| bin(-1) |
+------------------------------------------------------------------+
| 1111111111111111111111111111111111111111111111111111111111111101 |
| 1111111111111111111111111111111111111111111111111111111111111111 |
+------------------------------------------------------------------+
```

### keywords
BIN
```sql
select bin(123);
```

```text
+----------+
| bin(123) |
+----------+
| 1111011 |
+----------+
```
Original file line number Diff line number Diff line change
Expand Up @@ -22,36 +22,60 @@ specific language governing permissions and limitations
under the License.
-->

## cbrt
## Description

### description
#### Syntax
Calculate the cube root of the parameter

`DOUBLE cbrt(DOUBLE x)`
Returns the cube root of x.
## Syntax

### example
```sql
CBRT(<a>)
```

## Parameters

| Parameter | Description |
| -- | -- |
| `<a>` | Floating point parameter |

## Return Value

Cubic root of parameter `<a>`, a floating point number.

## Examples

```sql
select cbrt(0);
```

```text
+-------------------------+
| cbrt(cast(0 as DOUBLE)) |
+-------------------------+
| 0.0 |
+-------------------------+
```

```sql
select cbrt(-111);
```
mysql> select cbrt(8);
+-----------+
| cbrt(8.0) |
+-----------+
| 2 |
+-----------+
mysql> select cbrt(2.0);
+--------------------+
| cbrt(2.0) |
+--------------------+
| 1.2599210498948734 |
+--------------------+
mysql> select cbrt(-1000.0);
+---------------+
| cbrt(-1000.0) |
+---------------+
| -10 |
+---------------+

```text
+----------------------------+
| cbrt(cast(-111 as DOUBLE)) |
+----------------------------+
| -4.805895533705333 |
+----------------------------+
```

### keywords
CBRT
```sql
select cbrt(1234);
```

```text
+----------------------------+
| cbrt(cast(1234 as DOUBLE)) |
+----------------------------+
| 10.726014668827325 |
+----------------------------+
```
Original file line number Diff line number Diff line change
Expand Up @@ -22,66 +22,102 @@ specific language governing permissions and limitations
under the License.
-->

## ceil
## Description

### description
#### Syntax
Round up floating-point and fixed-point decimals to a specific number of places and return the rounded floating-point or fixed-point number.

`T ceil(T x[, d])`
## Syntax

If not specified `d`: returns the smallest integer value less than or equal to `x`, which is **the most common usage**.
Otherwise, returns the smallest round number that is less than or equal to `x` and flowing the rules:
```sql
CEIL(<a>[, <d>])
```

## Parameters

| Parameter | Description |
| -- | -- |
| `<a>` | Floating-point (Double) or fixed-point (Decimal) parameter indicating the parameter to be rounded |
| `<d>` | Optional, integer, indicates rounding to the target number of digits, a positive number means rounding to the next decimal point, a negative number means rounding to the next decimal point, and `0` indicates rounding to an integer. When not filled, it is equivalent to `<d> = 0`. |

## Return Value

Returns the smallest rounded number greater than or equal to `<a>` according to the following rules.

Round to `1/(10^d)` digit, i.e., make the result divisible by `1/(10^d)`. If `1/(10^d)` is not exact, the rounding digit is the nearest number of the corresponding data type.

If `d` is specified as literal:
`d` = 0: just like without `d`
`d` > 0 or `d` < 0: the round number would be a multiple of `1/(10^d)`, or the nearest number of the appropriate data type if `1/(10^d)` isn't exact.
For an entry `<a>` of type Decimal, assuming it is of type `Decimal(p, s)`, the return value is:

Else if `d` is a column, and `x` has Decimal type, scale of result Decimal will always be same with input Decimal.
- `Decimal(p, 0)`,if `<d> <= 0`
- `Decimal(p, <d>)`,if `0 < <d> <= s`
- `Decimal(p, s)`,if `<d> > s`

:::tip
The other alias for this function are `dceil` and `ceiling`.
:::
## Alias

### example
- DCEIL
- CEILING

## Examples

```sql
select ceil(123.456);
```

```text
+---------------+
| ceil(123.456) |
+---------------+
| 124 |
+---------------+
```
mysql> select ceil(1);
+-----------+
| ceil(1.0) |
+-----------+
| 1 |
+-----------+
mysql> select ceil(2.4);
+-----------+
| ceil(2.4) |
+-----------+
| 3 |
+-----------+
mysql> select ceil(-10.3);
+-------------+
| ceil(-10.3) |
+-------------+
| -10 |
+-------------+
mysql> select ceil(123.45, 1), ceil(123.45), ceil(123.45, 0), ceil(123.45, -1);

```sql
select ceil(123.456, 2);
```

```text
+------------------+
| ceil(123.456, 2) |
+------------------+
| 123.46 |
+------------------+
```

```sql
select ceil(123.456, -2);
```

```text
+-------------------+
| ceil(123.456, -2) |
+-------------------+
| 200 |
+-------------------+
```

```sql
select ceil(123.45, 1), ceil(123.45), ceil(123.45, 0), ceil(123.45, -1);
```

```text
+-----------------+--------------+-----------------+------------------+
| ceil(123.45, 1) | ceil(123.45) | ceil(123.45, 0) | ceil(123.45, -1) |
+-----------------+--------------+-----------------+------------------+
| 123.5 | 124 | 124 | 130 |
+-----------------+--------------+-----------------+------------------+
mysql> SELECT number
-> , ceil(number * 2.5, number - 1) AS c_decimal_column
-> , ceil(number * 2.5, 0) AS c_decimal_literal
-> , ceil(cast(number * 2.5 AS DOUBLE), number - 1) AS c_double_column
-> , ceil(cast(number * 2.5 AS DOUBLE), 0) AS c_double_literal
-> FROM test_enhanced_round
-> WHERE rid = 1;
+--------+------------------+-------------------+-----------------+------------------+
| number | c_decimal_column | c_decimal_literal | c_double_column | c_double_literal |
+--------+------------------+-------------------+-----------------+------------------+
| 1 | 3.0 | 3 | 3 | 3 |
+--------+------------------+-------------------+-----------------+------------------+
```

### keywords
CEIL, DCEIL, CEILING
```sql
select ceil(x, 2) from ( select cast(123.456 as decimal(6,3)) as x from numbers("number"="5") )t;
```

```text
+------------+
| ceil(x, 2) |
+------------+
| 123.46 |
| 123.46 |
| 123.46 |
| 123.46 |
| 123.46 |
+------------+
```
Original file line number Diff line number Diff line change
Expand Up @@ -22,41 +22,62 @@ specific language governing permissions and limitations
under the License.
-->

## conv
## Description

### description
#### Syntax
Do radix conversion for input parameter.

## Syntax

```sql
VARCHAR CONV(VARCHAR input, TINYINT from_base, TINYINT to_base)
VARCHAR CONV(BIGINT input, TINYINT from_base, TINYINT to_base)
CONV(<input>, <from_base>, <to_base>)
```
Convert the input number to the target base. The input base range should be within `[2,36]`.

### example
## Parameters

| Parameter | Description |
| -- | -- |
| `<input>` | Parameters to be converted, either as strings or integers |
| `<from_base>` | Numeric, the source base, within `[2,36]`. |
| `<to_base>` | Numeric, the target base, within `[2,36]`. |

## Return Value

The number under the converted target binary `<to_base>` is returned as a string.

## Examples

```sql
SELECT CONV(15,10,2);
```
MySQL [test]> SELECT CONV(15,10,2);

```text
+-----------------+
| conv(15, 10, 2) |
+-----------------+
| 1111 |
+-----------------+
```

MySQL [test]> SELECT CONV('ff',16,10);
```sql
SELECT CONV('ff',16,10);
```

```text
+--------------------+
| conv('ff', 16, 10) |
+--------------------+
| 255 |
+--------------------+
```

MySQL [test]> SELECT CONV(230,10,16);
```sql
SELECT CONV(230,10,16);
```

```text
+-------------------+
| conv(230, 10, 16) |
+-------------------+
| E6 |
+-------------------+
```

### keywords
CONV
Loading
Loading