Skip to content

Apache DataFusion SQL Query Engine

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

Andrew Lamba3088be03f3e
diff --git a/datafusion/core/tests/sqllogictests/test_files/scalar.slt b/datafusion/core/tests/sqllogictests/test_files/scalar.slt
index 6e563a671d..d5ce7737fb 100644
--- a/datafusion/core/tests/sqllogictests/test_files/scalar.slt
+++ b/datafusion/core/tests/sqllogictests/test_files/scalar.slt
@@ -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
@@ -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
@@ -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
diff --git a/docs/source/user-guide/sql/scalar_functions.md b/docs/source/user-guide/sql/scalar_functions.md
index bcdd383252..301f57d031 100644
--- a/docs/source/user-guide/sql/scalar_functions.md
+++ b/docs/source/user-guide/sql/scalar_functions.md
@@ -504,10 +504,10 @@ 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
@@ -515,6 +515,12 @@ trunc(numeric_expression)
- **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)