Selaa lähdekoodia

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
Marios Trivyzas 6 vuotta sitten
vanhempi
commit
d95d885bae

+ 37 - 0
docs/reference/sql/functions/conditional.asciidoc

@@ -81,3 +81,40 @@ include-tagged::{sql-specs}/docs.csv-spec[ifNullReturnFirst]
 ----
 include-tagged::{sql-specs}/docs.csv-spec[ifNullReturnSecond]
 ----
+
+
+[[sql-functions-conditional-isnull]]
+==== `ISNULL`
+
+.Synopsis
+[source, sql]
+----
+ISNULL ( 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[isNullReturnFirst]
+----
+
+["source","sql",subs="attributes,callouts,macros"]
+----
+include-tagged::{sql-specs}/docs.csv-spec[isNullReturnSecond]
+----

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

@@ -21,7 +21,8 @@ SUM_OF_SQUARES  |AGGREGATE
 VAR_POP         |AGGREGATE      
 COALESCE        |CONDITIONAL
 IFNULL          |CONDITIONAL
-DAY             |SCALAR         
+ISNULL          |CONDITIONAL
+DAY             |SCALAR
 DAYNAME         |SCALAR         
 DAYOFMONTH      |SCALAR         
 DAYOFWEEK       |SCALAR         

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

@@ -198,7 +198,8 @@ SUM_OF_SQUARES  |AGGREGATE
 VAR_POP         |AGGREGATE      
 COALESCE        |CONDITIONAL
 IFNULL          |CONDITIONAL
-DAY             |SCALAR         
+ISNULL          |CONDITIONAL
+DAY             |SCALAR
 DAYNAME         |SCALAR         
 DAYOFMONTH      |SCALAR         
 DAYOFWEEK       |SCALAR         
@@ -1553,3 +1554,25 @@ SELECT IFNULL(null, 'search') AS "ifnull";
 search
 // end::ifNullReturnSecond
 ;
+
+
+isNullReturnFirst
+// tag::isNullReturnFirst
+SELECT ISNULL('elastic', null) AS "isnull";
+
+    isnull
+---------------
+elastic
+// end::isNullReturnFirst
+;
+
+
+isNullReturnSecond
+// tag::isNullReturnSecond
+SELECT ISNULL(null, 'search') AS "isnull";
+
+    isnull
+---------------
+search
+// end::isNullReturnSecond
+;

+ 1 - 1
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));
+        addToMap(def(IFNull.class, IFNull::new, "ISNULL"));
         // Date
         addToMap(def(DayName.class, DayName::new, "DAYNAME"),
                 def(DayOfMonth.class, DayOfMonth::new, "DAYOFMONTH", "DAY", "DOM"),