alamb commented on code in PR #7042: URL: https://github.com/apache/arrow-datafusion/pull/7042#discussion_r1275382805
########## datafusion/core/tests/sqllogictests/test_files/scalar.slt: ########## @@ -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 Review Comment: Added ########## docs/source/user-guide/sql/scalar_functions.md: ########## @@ -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. Review Comment: 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 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
