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

Minor: add test + docs for 2 argument trunc with columns #7042

Merged
merged 7 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
18 changes: 13 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,15 @@ 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 columns and precision
query RRR rowsort
select trunc(a, 3) as a3, trunc(a, 1) as a1, trunc(arrow_cast(a, 'Float64'), 3) as a3_f64 from small_floats;
----
-0.7 -0.7 -0.7
-1 -1 -1
0.2 0.2 0.2
0.5 0.5 0.5
Copy link
Member

Choose a reason for hiding this comment

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

Hmm, as a has at most 1 decimal point, trunc(a, 3) or trunc(a, 1) basically cannot show the difference. I think it doesn't actually test what we want?

Copy link
Contributor

Choose a reason for hiding this comment

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

maybe add some long decimal point float number

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added


## bitwise and

# bitwise and with column and scalar
Expand Down Expand Up @@ -1497,7 +1506,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 +1520,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
5 changes: 4 additions & 1 deletion docs/source/user-guide/sql/scalar_functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -507,14 +507,17 @@ tanh(numeric_expression)
Truncates a number toward zero (at the decimal point).

```
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.
Copy link
Member

Choose a reason for hiding this comment

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

Doc looks good to me. Just one question. Can decimal_places be negative?

Copy link
Contributor

Choose a reason for hiding this comment

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

can not be negative, modify the trunc function just meet the self-defined precision of decimal to '.'

Copy link
Contributor Author

Choose a reason for hiding this comment

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

TIL turns out that negative precision means "truncate to the left of the decimal place": https://www.postgresqltutorial.com/postgresql-math-functions/postgresql-trunc/#:~:text=If%20the%20precision%20argument%20is,The%20precision%20argument%20is%20optional.

postgres=# select trunc(12345.6789, -2);
 trunc
-------
 12300
(1 row)

This is actually what the current datafusion implementation does too so I have added a test and updated the docs


## Conditional Functions

- [coalesce](#coalesce)
Expand Down