Skip to content

Commit

Permalink
SQL: Add TRUNC alias for TRUNCATE (#49571)
Browse files Browse the repository at this point in the history
Add TRUNC as alias to already implemented TRUNCATE
numeric function which is the flavour supported by
Oracle and PostgreSQL.

Relates to: #41195
  • Loading branch information
matriv committed Nov 26, 2019
1 parent 6a853b9 commit f2aa7f0
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 12 deletions.
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`

.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

0 comments on commit f2aa7f0

Please sign in to comment.