Skip to content

Commit

Permalink
Minor: add test + docs for 2 argument trunc with columns (#7042)
Browse files Browse the repository at this point in the history
* Minor: add test for 2 argument trunc with columns

* Update docs

* Add longer numbers to truncate

* add test for truncate with negative precision

* update docs with more details about precision

* prettier
  • Loading branch information
alamb authored Jul 27, 2023
1 parent 60ea6de commit a3088be
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
28 changes: 23 additions & 5 deletions datafusion/core/tests/sqllogictests/test_files/scalar.slt
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,25 @@ select trunc(4.267, 3), trunc(1.1234, 2), trunc(-1.1231, 6), trunc(1.2837284, 2)
----
4.267 1.12 -1.1231 1.28 1

# trunc with negative precision should truncate digits left of decimal
query R
select trunc(12345.678, -3);
----
12000

# trunc with columns and precision
query RRR rowsort
select
trunc(sqrt(abs(a)), 3) as a3,
trunc(sqrt(abs(a)), 1) as a1,
trunc(arrow_cast(sqrt(abs(a)), 'Float64'), 3) as a3_f64
from small_floats;
----
0.447 0.4 0.447
0.707 0.7 0.707
0.837 0.8 0.837
1 1 1

## bitwise and

# bitwise and with column and scalar
Expand Down Expand Up @@ -1497,7 +1516,6 @@ true true false true true true
# csv query boolean gt gt eq
query BBBBBB rowsort
SELECT a, b, a > b as gt, b = true as gt_scalar, a >= b as gt_eq, a >= true as gt_eq_scalar FROM t1
----
----
NULL NULL NULL NULL NULL NULL
NULL false NULL false NULL NULL
Expand All @@ -1512,10 +1530,10 @@ true true false true true true
# csv query boolean distinct from
query BBBBBB rowsort
SELECT a, b,
a is distinct from b as df,
b is distinct from true as df_scalar,
a is not distinct from b as ndf,
a is not distinct from true as ndf_scalar
a is distinct from b as df,
b is distinct from true as df_scalar,
a is not distinct from b as ndf,
a is not distinct from true as ndf_scalar
FROM t1
----
NULL NULL false true true false
Expand Down
10 changes: 8 additions & 2 deletions docs/source/user-guide/sql/scalar_functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -504,17 +504,23 @@ tanh(numeric_expression)

### `trunc`

Truncates a number toward zero (at the decimal point).
Truncates a number to a whole number or truncated to the specified decimal places.

```
trunc(numeric_expression)
trunc(numeric_expression[, decimal_places])
```

#### Arguments

- **numeric_expression**: Numeric expression to operate on.
Can be a constant, column, or function, and any combination of arithmetic operators.

- **decimal_places**: Optional. The number of decimal places to
truncate to. Defaults to 0 (truncate to a whole number). If
`decimal_places` is a positive integer, truncates digits to the
right of the decimal point. If `decimal_places` is a negative
integer, replaces digits to the left of the decimal point with `0`.

## Conditional Functions

- [coalesce](#coalesce)
Expand Down

0 comments on commit a3088be

Please sign in to comment.