Skip to content

Commit

Permalink
SQL: Implement NVL(expr1, expr2) (#35794)
Browse files Browse the repository at this point in the history
Add NVL as alias to IFNULL as they have the same
behaviour. Add basic tests and docs.

Closes: #35782
  • Loading branch information
matriv authored Nov 22, 2018
1 parent 11052b7 commit 92acf47
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 4 deletions.
37 changes: 37 additions & 0 deletions docs/reference/sql/functions/conditional.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,40 @@ include-tagged::{sql-specs}/docs.csv-spec[isNullReturnFirst]
----
include-tagged::{sql-specs}/docs.csv-spec[isNullReturnSecond]
----


[[sql-functions-conditional-nvl]]
==== `NVL`

.Synopsis
[source, sql]
----
NVL ( expression<1>, expression<2> )
----

*Input*:

<1> 1st expression

<2> 2nd expression


*Output*: 2nd expression if 1st expression is null, otherwise 1st expression.

.Description

Variant of <<sql-functions-conditional-coalesce>> with only two arguments.
Returns the first of its arguments that is not null.
If all arguments are null, then it returns `null`.



["source","sql",subs="attributes,callouts,macros"]
----
include-tagged::{sql-specs}/docs.csv-spec[nvlReturnFirst]
----

["source","sql",subs="attributes,callouts,macros"]
----
include-tagged::{sql-specs}/docs.csv-spec[nvlReturnSecond]
----
3 changes: 2 additions & 1 deletion x-pack/plugin/sql/qa/src/main/resources/command.csv-spec
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ VAR_POP |AGGREGATE
COALESCE |CONDITIONAL
IFNULL |CONDITIONAL
ISNULL |CONDITIONAL
NVL |CONDITIONAL
DAY |SCALAR
DAYNAME |SCALAR
DAYNAME |SCALAR
DAYOFMONTH |SCALAR
DAYOFWEEK |SCALAR
DAYOFYEAR |SCALAR
Expand Down
25 changes: 23 additions & 2 deletions x-pack/plugin/sql/qa/src/main/resources/docs.csv-spec
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,9 @@ VAR_POP |AGGREGATE
COALESCE |CONDITIONAL
IFNULL |CONDITIONAL
ISNULL |CONDITIONAL
NVL |CONDITIONAL
DAY |SCALAR
DAYNAME |SCALAR
DAYNAME |SCALAR
DAYOFMONTH |SCALAR
DAYOFWEEK |SCALAR
DAYOFYEAR |SCALAR
Expand Down Expand Up @@ -1555,7 +1556,6 @@ search
// end::ifNullReturnSecond
;


isNullReturnFirst
// tag::isNullReturnFirst
SELECT ISNULL('elastic', null) AS "isnull";
Expand All @@ -1576,3 +1576,24 @@ SELECT ISNULL(null, 'search') AS "isnull";
search
// end::isNullReturnSecond
;

nvlReturnFirst
// tag::nvlReturnFirst
SELECT NVL('elastic', null) AS "nvl";

nvl
---------------
elastic
// end::nvlReturnFirst
;


nvlReturnSecond
// tag::nvlReturnSecond
SELECT NVL(null, 'search') AS "nvl";

nvl
---------------
search
// end::nvlReturnSecond
;
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ private void defineDefaultFunctions() {
// Scalar functions
// conditional
addToMap(def(Coalesce.class, Coalesce::new));
addToMap(def(IFNull.class, IFNull::new, "ISNULL"));
addToMap(def(IFNull.class, IFNull::new, "ISNULL", "NVL"));
// Date
addToMap(def(DayName.class, DayName::new, "DAYNAME"),
def(DayOfMonth.class, DayOfMonth::new, "DAYOFMONTH", "DAY", "DOM"),
Expand Down

0 comments on commit 92acf47

Please sign in to comment.