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

SQL: Add TRUNC alias for TRUNCATE #49571

Merged
merged 1 commit into from
Nov 26, 2019
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
2 changes: 1 addition & 1 deletion docs/reference/sql/functions/math.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ include-tagged::{sql-specs}/docs/docs.csv-spec[mathInlineSqrt]
--------------------------------------------------

[[sql-functions-math-truncate]]
==== `TRUNCATE`
==== `TRUNCATE/TRUNC`
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't be TRUNC/TRUNCATE (alphabetical order)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In general, we don't do it alphabetically here, for example: DAY_OF_MONTH/DOM/DAY
@astefan, what do you think?

Copy link
Contributor

Choose a reason for hiding this comment

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

Seems indeed a mixed usage: CEIL/CEILING, but also RANDOM/RAND.
Fwiw, I find the "short/long" version easier to read (i.e. visually take in), but unless we order them all like that, this one case is not very relevant.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was thinking also that TRUNCATE was already there, if we put TRUNC first we will need to also change the definition paragraph and use TRUNC instead (we always use the 1st version there).

Copy link
Contributor

Choose a reason for hiding this comment

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

The initial implementation for RANDOM and TRUNCATE came from https://docs.microsoft.com/en-us/sql/odbc/reference/appendixes/numeric-functions?view=sql-server-2017, where they are indeed called like that. I agree with Marios here: in our docs the first name is the "main" one and the rest are aliases. For consistency, I would go with TRUNCATE/TRUNC, RANDOM/RAND etc. The biggest counter-example to the "alphabetical" approach is the series of DOM/DOY/DOW/DAYNAME aliases for some of the date/datetime functions.


.Synopsis:
[source, sql]
Expand Down
5 changes: 3 additions & 2 deletions x-pack/plugin/sql/qa/src/main/resources/command.csv-spec
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,9 @@ SIN |SCALAR
SINH |SCALAR
SQRT |SCALAR
TAN |SCALAR
TRUNCATE |SCALAR
ASCII |SCALAR
TRUNC |SCALAR
TRUNCATE |SCALAR
ASCII |SCALAR
BIT_LENGTH |SCALAR
CHAR |SCALAR
CHARACTER_LENGTH |SCALAR
Expand Down
7 changes: 4 additions & 3 deletions x-pack/plugin/sql/qa/src/main/resources/docs/docs.csv-spec
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,9 @@ SIN |SCALAR
SINH |SCALAR
SQRT |SCALAR
TAN |SCALAR
TRUNCATE |SCALAR
ASCII |SCALAR
TRUNC |SCALAR
TRUNCATE |SCALAR
ASCII |SCALAR
BIT_LENGTH |SCALAR
CHAR |SCALAR
CHARACTER_LENGTH |SCALAR
Expand Down Expand Up @@ -2032,7 +2033,7 @@ SELECT TRUNCATE(-345.153, -1) AS trimmed;

mathTruncateWithPositiveParameter
// tag::mathTruncateWithPositiveParameter
SELECT TRUNCATE(-345.153, 1) AS trimmed;
SELECT TRUNC(-345.153, 1) AS trimmed;

trimmed
---------------
Expand Down
10 changes: 5 additions & 5 deletions x-pack/plugin/sql/qa/src/main/resources/math.csv-spec
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

// this one doesn't work in H2 at all
truncateWithAsciiHavingAndOrderBy
SELECT TRUNCATE(ASCII(LEFT(first_name, 1)), 1), COUNT(*) count FROM test_emp GROUP BY ASCII(LEFT(first_name, 1)) HAVING COUNT(*) > 5 ORDER BY TRUNCATE(ASCII(LEFT(first_name, 1)), 1) DESC;
SELECT TRUNC(ASCII(LEFT(first_name, 1)), 1), COUNT(*) count FROM test_emp GROUP BY ASCII(LEFT(first_name, 1)) HAVING COUNT(*) > 5 ORDER BY TRUNCATE(ASCII(LEFT(first_name, 1)), 1) DESC;

TRUNCATE(ASCII(LEFT(first_name, 1)), 1):i| count:l
TRUNC(ASCII(LEFT(first_name, 1)), 1):i | count:l
-----------------------------------------+---------------
null |10
66 |7
Expand Down Expand Up @@ -45,7 +45,7 @@ SELECT ROUND(salary, 2) ROUNDED, salary FROM test_emp GROUP BY ROUNDED, salary O
;

truncateWithGroupByAndOrderBy
SELECT TRUNCATE(salary, 2) TRUNCATED, salary FROM test_emp GROUP BY TRUNCATED, salary ORDER BY TRUNCATED LIMIT 10;
SELECT TRUNC(salary, 2) TRUNCATED, salary FROM test_emp GROUP BY TRUNCATED, salary ORDER BY TRUNCATED LIMIT 10;

TRUNCATED | salary
---------------+---------------
Expand Down Expand Up @@ -129,9 +129,9 @@ SELECT MIN(salary) mi, MAX(salary) ma, YEAR(hire_date) year, ROUND(AVG(languages
;

groupByAndOrderByTruncateWithPositiveParameter
SELECT TRUNCATE(AVG(salary),2), AVG(salary), COUNT(*) FROM test_emp GROUP BY TRUNCATE(salary, 2) ORDER BY TRUNCATE(salary, 2) DESC LIMIT 10;
SELECT TRUNC(AVG(salary),2), AVG(salary), COUNT(*) FROM test_emp GROUP BY TRUNC(salary, 2) ORDER BY TRUNCATE(salary, 2) DESC LIMIT 10;

TRUNCATE(AVG(salary),2):d| AVG(salary):d | COUNT(*):l
TRUNC(AVG(salary),2):d | AVG(salary):d | COUNT(*):l
-------------------------+---------------+---------------
74999.0 |74999.0 |1
74970.0 |74970.0 |1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ private void defineDefaultFunctions() {
def(Sinh.class, Sinh::new, "SINH"),
def(Sqrt.class, Sqrt::new, "SQRT"),
def(Tan.class, Tan::new, "TAN"),
def(Truncate.class, Truncate::new, "TRUNCATE"));
def(Truncate.class, Truncate::new, "TRUNCATE", "TRUNC"));
// String
addToMap(def(Ascii.class, Ascii::new, "ASCII"),
def(BitLength.class, BitLength::new, "BIT_LENGTH"),
Expand Down