diff --git a/docs/reference/sql/functions/conditional.asciidoc b/docs/reference/sql/functions/conditional.asciidoc index 944b42bfdcd..75782b894af 100644 --- a/docs/reference/sql/functions/conditional.asciidoc +++ b/docs/reference/sql/functions/conditional.asciidoc @@ -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 <> 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] +---- diff --git a/x-pack/plugin/sql/qa/src/main/resources/command.csv-spec b/x-pack/plugin/sql/qa/src/main/resources/command.csv-spec index 7cfeb0e3628..62f418dce2b 100644 --- a/x-pack/plugin/sql/qa/src/main/resources/command.csv-spec +++ b/x-pack/plugin/sql/qa/src/main/resources/command.csv-spec @@ -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 diff --git a/x-pack/plugin/sql/qa/src/main/resources/docs.csv-spec b/x-pack/plugin/sql/qa/src/main/resources/docs.csv-spec index 928cc3b38c6..c8410e6c9ea 100644 --- a/x-pack/plugin/sql/qa/src/main/resources/docs.csv-spec +++ b/x-pack/plugin/sql/qa/src/main/resources/docs.csv-spec @@ -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 @@ -1555,7 +1556,6 @@ search // end::ifNullReturnSecond ; - isNullReturnFirst // tag::isNullReturnFirst SELECT ISNULL('elastic', null) AS "isnull"; @@ -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 +; diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/FunctionRegistry.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/FunctionRegistry.java index ef8e55de6d5..077b5cab78f 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/FunctionRegistry.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/FunctionRegistry.java @@ -146,7 +146,7 @@ public class FunctionRegistry { // 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"),