Skip to content

Commit 3c69d4d

Browse files
committed
SQL: Add TRUNC alias for TRUNCATE (#49571)
Add TRUNC as alias to already implemented TRUNCATE numeric function which is the flavour supported by Oracle and PostgreSQL. Relates to: #41195 (cherry picked from commit f2aa7f0)
1 parent 048b9db commit 3c69d4d

File tree

5 files changed

+14
-12
lines changed

5 files changed

+14
-12
lines changed

docs/reference/sql/functions/math.asciidoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ include-tagged::{sql-specs}/docs/docs.csv-spec[mathInlineSqrt]
385385
--------------------------------------------------
386386

387387
[[sql-functions-math-truncate]]
388-
==== `TRUNCATE`
388+
==== `TRUNCATE/TRUNC`
389389

390390
.Synopsis:
391391
[source, sql]

x-pack/plugin/sql/qa/src/main/resources/command.csv-spec

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,9 @@ SIN |SCALAR
122122
SINH |SCALAR
123123
SQRT |SCALAR
124124
TAN |SCALAR
125-
TRUNCATE |SCALAR
126-
ASCII |SCALAR
125+
TRUNC |SCALAR
126+
TRUNCATE |SCALAR
127+
ASCII |SCALAR
127128
BIT_LENGTH |SCALAR
128129
CHAR |SCALAR
129130
CHARACTER_LENGTH |SCALAR

x-pack/plugin/sql/qa/src/main/resources/docs/docs.csv-spec

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,9 @@ SIN |SCALAR
318318
SINH |SCALAR
319319
SQRT |SCALAR
320320
TAN |SCALAR
321-
TRUNCATE |SCALAR
322-
ASCII |SCALAR
321+
TRUNC |SCALAR
322+
TRUNCATE |SCALAR
323+
ASCII |SCALAR
323324
BIT_LENGTH |SCALAR
324325
CHAR |SCALAR
325326
CHARACTER_LENGTH |SCALAR
@@ -2031,7 +2032,7 @@ SELECT TRUNCATE(-345.153, -1) AS trimmed;
20312032

20322033
mathTruncateWithPositiveParameter
20332034
// tag::mathTruncateWithPositiveParameter
2034-
SELECT TRUNCATE(-345.153, 1) AS trimmed;
2035+
SELECT TRUNC(-345.153, 1) AS trimmed;
20352036

20362037
trimmed
20372038
---------------

x-pack/plugin/sql/qa/src/main/resources/math.csv-spec

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
// this one doesn't work in H2 at all
44
truncateWithAsciiHavingAndOrderBy
5-
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;
5+
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;
66

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

4747
truncateWithGroupByAndOrderBy
48-
SELECT TRUNCATE(salary, 2) TRUNCATED, salary FROM test_emp GROUP BY TRUNCATED, salary ORDER BY TRUNCATED LIMIT 10;
48+
SELECT TRUNC(salary, 2) TRUNCATED, salary FROM test_emp GROUP BY TRUNCATED, salary ORDER BY TRUNCATED LIMIT 10;
4949

5050
TRUNCATED | salary
5151
---------------+---------------
@@ -129,9 +129,9 @@ SELECT MIN(salary) mi, MAX(salary) ma, YEAR(hire_date) year, ROUND(AVG(languages
129129
;
130130

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

134-
TRUNCATE(AVG(salary),2):d| AVG(salary):d | COUNT(*):l
134+
TRUNC(AVG(salary),2):d | AVG(salary):d | COUNT(*):l
135135
-------------------------+---------------+---------------
136136
74999.0 |74999.0 |1
137137
74970.0 |74970.0 |1

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/FunctionRegistry.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ private void defineDefaultFunctions() {
242242
def(Sinh.class, Sinh::new, "SINH"),
243243
def(Sqrt.class, Sqrt::new, "SQRT"),
244244
def(Tan.class, Tan::new, "TAN"),
245-
def(Truncate.class, Truncate::new, "TRUNCATE"));
245+
def(Truncate.class, Truncate::new, "TRUNCATE", "TRUNC"));
246246
// String
247247
addToMap(def(Ascii.class, Ascii::new, "ASCII"),
248248
def(BitLength.class, BitLength::new, "BIT_LENGTH"),

0 commit comments

Comments
 (0)