Skip to content

Commit d95d885

Browse files
authored
SQL: Implement ISNULL(expr1, expr2) (#35793)
Add ISNULL as an alias of IFNULL as they have the same behaviour. Add basic test and docs. Closes: #35781
1 parent 3548d6a commit d95d885

File tree

4 files changed

+64
-3
lines changed

4 files changed

+64
-3
lines changed

docs/reference/sql/functions/conditional.asciidoc

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,40 @@ include-tagged::{sql-specs}/docs.csv-spec[ifNullReturnFirst]
8181
----
8282
include-tagged::{sql-specs}/docs.csv-spec[ifNullReturnSecond]
8383
----
84+
85+
86+
[[sql-functions-conditional-isnull]]
87+
==== `ISNULL`
88+
89+
.Synopsis
90+
[source, sql]
91+
----
92+
ISNULL ( expression<1>, expression<2> )
93+
----
94+
95+
*Input*:
96+
97+
<1> 1st expression
98+
99+
<2> 2nd expression
100+
101+
102+
*Output*: 2nd expression if 1st expression is null, otherwise 1st expression.
103+
104+
.Description
105+
106+
Variant of <<sql-functions-conditional-coalesce>> with only two arguments.
107+
Returns the first of its arguments that is not null.
108+
If all arguments are null, then it returns `null`.
109+
110+
111+
112+
["source","sql",subs="attributes,callouts,macros"]
113+
----
114+
include-tagged::{sql-specs}/docs.csv-spec[isNullReturnFirst]
115+
----
116+
117+
["source","sql",subs="attributes,callouts,macros"]
118+
----
119+
include-tagged::{sql-specs}/docs.csv-spec[isNullReturnSecond]
120+
----

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ SUM_OF_SQUARES |AGGREGATE
2121
VAR_POP |AGGREGATE
2222
COALESCE |CONDITIONAL
2323
IFNULL |CONDITIONAL
24-
DAY |SCALAR
24+
ISNULL |CONDITIONAL
25+
DAY |SCALAR
2526
DAYNAME |SCALAR
2627
DAYOFMONTH |SCALAR
2728
DAYOFWEEK |SCALAR

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,8 @@ SUM_OF_SQUARES |AGGREGATE
198198
VAR_POP |AGGREGATE
199199
COALESCE |CONDITIONAL
200200
IFNULL |CONDITIONAL
201-
DAY |SCALAR
201+
ISNULL |CONDITIONAL
202+
DAY |SCALAR
202203
DAYNAME |SCALAR
203204
DAYOFMONTH |SCALAR
204205
DAYOFWEEK |SCALAR
@@ -1553,3 +1554,25 @@ SELECT IFNULL(null, 'search') AS "ifnull";
15531554
search
15541555
// end::ifNullReturnSecond
15551556
;
1557+
1558+
1559+
isNullReturnFirst
1560+
// tag::isNullReturnFirst
1561+
SELECT ISNULL('elastic', null) AS "isnull";
1562+
1563+
isnull
1564+
---------------
1565+
elastic
1566+
// end::isNullReturnFirst
1567+
;
1568+
1569+
1570+
isNullReturnSecond
1571+
// tag::isNullReturnSecond
1572+
SELECT ISNULL(null, 'search') AS "isnull";
1573+
1574+
isnull
1575+
---------------
1576+
search
1577+
// end::isNullReturnSecond
1578+
;

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
@@ -146,7 +146,7 @@ private void defineDefaultFunctions() {
146146
// Scalar functions
147147
// conditional
148148
addToMap(def(Coalesce.class, Coalesce::new));
149-
addToMap(def(IFNull.class, IFNull::new));
149+
addToMap(def(IFNull.class, IFNull::new, "ISNULL"));
150150
// Date
151151
addToMap(def(DayName.class, DayName::new, "DAYNAME"),
152152
def(DayOfMonth.class, DayOfMonth::new, "DAYOFMONTH", "DAY", "DOM"),

0 commit comments

Comments
 (0)