Pārlūkot izejas kodu

ESQL: Add type tables for operators to docs (#103206)

This adds a tiny blurb for each operator to the docs with a railroad
diagram of the operator's syntax and a table of the input and output
types. This also fixes the tests to correctly generate the tables for
operators.
Nik Everett 1 gadu atpakaļ
vecāks
revīzija
3b738905e1

+ 80 - 8
docs/reference/esql/functions/binary.asciidoc

@@ -2,19 +2,91 @@
 [[esql-binary-operators]]
 === Binary operators
 
-These binary comparison operators are supported:
+[[esql-binary-operators-equality]]
+==== Equality
+[.text-center]
+image::esql/functions/signature/equals.svg[Embedded,opts=inline]
+
+Supported types:
+
+include::types/equals.asciidoc[]
+
+==== Inequality `!=`
+[.text-center]
+image::esql/functions/signature/not_equals.svg[Embedded,opts=inline]
 
-* equality: `==`
-* inequality: `!=`
-* less than: `<`
-* less than or equal: `<=`
-* larger than: `>`
-* larger than or equal: `>=`
+Supported types:
 
-And these mathematical operators are supported:
+include::types/not_equals.asciidoc[]
 
+==== Less than `<`
+[.text-center]
+image::esql/functions/signature/less_than.svg[Embedded,opts=inline]
+
+Supported types:
+
+include::types/less_than.asciidoc[]
+
+==== Less than or equal to `<=`
+[.text-center]
+image::esql/functions/signature/less_than_or_equal.svg[Embedded,opts=inline]
+
+Supported types:
+
+include::types/less_than_or_equal.asciidoc[]
+
+==== Greater than `>`
+[.text-center]
+image::esql/functions/signature/greater_than.svg[Embedded,opts=inline]
+
+Supported types:
+
+include::types/greater_than.asciidoc[]
+
+==== Greater than or equal to `>=`
+[.text-center]
+image::esql/functions/signature/greater_than_or_equal.svg[Embedded,opts=inline]
+
+Supported types:
+
+include::types/greater_than_or_equal.asciidoc[]
+
+==== Add `+`
 [.text-center]
 image::esql/functions/signature/add.svg[Embedded,opts=inline]
 
+Supported types:
+
+include::types/add.asciidoc[]
+
+==== Subtract `-`
 [.text-center]
 image::esql/functions/signature/sub.svg[Embedded,opts=inline]
+
+Supported types:
+
+include::types/sub.asciidoc[]
+
+==== Multiply `*`
+[.text-center]
+image::esql/functions/signature/mul.svg[Embedded,opts=inline]
+
+Supported types:
+
+include::types/mul.asciidoc[]
+
+==== Divide `/`
+[.text-center]
+image::esql/functions/signature/div.svg[Embedded,opts=inline]
+
+Supported types:
+
+include::types/div.asciidoc[]
+
+==== Modulus `%`
+[.text-center]
+image::esql/functions/signature/mod.svg[Embedded,opts=inline]
+
+Supported types:
+
+include::types/mod.asciidoc[]

+ 1 - 0
docs/reference/esql/functions/signature/greater_than_or_equal.svg

@@ -0,0 +1 @@
+<svg version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg" width="186" height="46" viewbox="0 0 186 46"><defs><style type="text/css">#guide .c{fill:none;stroke:#222222;}#guide .j{fill:#000000;font-family:Roboto Mono,Sans-serif;font-size:20px;}#guide .l{fill:#e4f4ff;stroke:#222222;}#guide .syn{fill:#8D8D8D;font-family:Roboto Mono,Sans-serif;font-size:20px;}</style></defs><path class="c" d="M0 31h5m56 0h10m44 0h10m56 0h5"/><rect class="l" x="5" y="5" width="56" height="36" rx="7"/><text class="j" x="15" y="31">lhs</text><rect class="l" x="71" y="5" width="44" height="36" rx="7"/><text class="syn" x="81" y="31">&gt;=</text><rect class="l" x="125" y="5" width="56" height="36" rx="7"/><text class="j" x="135" y="31">rhs</text></svg>

+ 1 - 0
docs/reference/esql/functions/signature/less_than_or_equal.svg

@@ -0,0 +1 @@
+<svg version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg" width="186" height="46" viewbox="0 0 186 46"><defs><style type="text/css">#guide .c{fill:none;stroke:#222222;}#guide .j{fill:#000000;font-family:Roboto Mono,Sans-serif;font-size:20px;}#guide .l{fill:#e4f4ff;stroke:#222222;}#guide .syn{fill:#8D8D8D;font-family:Roboto Mono,Sans-serif;font-size:20px;}</style></defs><path class="c" d="M0 31h5m56 0h10m44 0h10m56 0h5"/><rect class="l" x="5" y="5" width="56" height="36" rx="7"/><text class="j" x="15" y="31">lhs</text><rect class="l" x="71" y="5" width="44" height="36" rx="7"/><text class="syn" x="81" y="31">&lt;=</text><rect class="l" x="125" y="5" width="56" height="36" rx="7"/><text class="j" x="135" y="31">rhs</text></svg>

+ 0 - 1
docs/reference/esql/functions/signature/to_degrees.svg

@@ -1 +0,0 @@
-<svg version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg" width="276" height="46" viewbox="0 0 276 46"><defs><style type="text/css">#guide .c{fill:none;stroke:#222222;}#guide .k{fill:#000000;font-family:Roboto Mono,Sans-serif;font-size:20px;}#guide .s{fill:#e4f4ff;stroke:#222222;}#guide .syn{fill:#8D8D8D;font-family:Roboto Mono,Sans-serif;font-size:20px;}</style></defs><path class="c" d="M0 31h5m140 0h10m32 0h10m32 0h10m32 0h5"/><rect class="s" x="5" y="5" width="140" height="36"/><text class="k" x="15" y="31">TO_DEGREES</text><rect class="s" x="155" y="5" width="32" height="36" rx="7"/><text class="syn" x="165" y="31">(</text><rect class="s" x="197" y="5" width="32" height="36" rx="7"/><text class="k" x="207" y="31">v</text><rect class="s" x="239" y="5" width="32" height="36" rx="7"/><text class="syn" x="249" y="31">)</text></svg>

+ 12 - 0
docs/reference/esql/functions/types/add.asciidoc

@@ -0,0 +1,12 @@
+[%header.monospaced.styled,format=dsv,separator=|]
+|===
+lhs | rhs | result
+date_period | date_period | date_period
+date_period | datetime | datetime
+datetime | date_period | datetime
+datetime | time_duration | datetime
+double | double | double
+integer | integer | integer
+long | long | long
+time_duration | time_duration | time_duration
+|===

+ 7 - 0
docs/reference/esql/functions/types/div.asciidoc

@@ -0,0 +1,7 @@
+[%header.monospaced.styled,format=dsv,separator=|]
+|===
+lhs | rhs | result
+double | double | double
+integer | integer | integer
+long | long | long
+|===

+ 5 - 0
docs/reference/esql/functions/types/equals.asciidoc

@@ -0,0 +1,5 @@
+[%header.monospaced.styled,format=dsv,separator=|]
+|===
+lhs | rhs | result
+integer | integer | boolean
+|===

+ 5 - 0
docs/reference/esql/functions/types/greater_than.asciidoc

@@ -0,0 +1,5 @@
+[%header.monospaced.styled,format=dsv,separator=|]
+|===
+lhs | rhs | result
+integer | integer | boolean
+|===

+ 5 - 0
docs/reference/esql/functions/types/greater_than_or_equal.asciidoc

@@ -0,0 +1,5 @@
+[%header.monospaced.styled,format=dsv,separator=|]
+|===
+lhs | rhs | result
+integer | integer | boolean
+|===

+ 5 - 0
docs/reference/esql/functions/types/less_than.asciidoc

@@ -0,0 +1,5 @@
+[%header.monospaced.styled,format=dsv,separator=|]
+|===
+lhs | rhs | result
+integer | integer | boolean
+|===

+ 5 - 0
docs/reference/esql/functions/types/less_than_or_equal.asciidoc

@@ -0,0 +1,5 @@
+[%header.monospaced.styled,format=dsv,separator=|]
+|===
+lhs | rhs | result
+integer | integer | boolean
+|===

+ 7 - 0
docs/reference/esql/functions/types/mod.asciidoc

@@ -0,0 +1,7 @@
+[%header.monospaced.styled,format=dsv,separator=|]
+|===
+lhs | rhs | result
+double | double | double
+integer | integer | integer
+long | long | long
+|===

+ 7 - 0
docs/reference/esql/functions/types/mul.asciidoc

@@ -0,0 +1,7 @@
+[%header.monospaced.styled,format=dsv,separator=|]
+|===
+lhs | rhs | result
+double | double | double
+integer | integer | integer
+long | long | long
+|===

+ 4 - 3
docs/reference/esql/functions/types/to_degrees.asciidoc → docs/reference/esql/functions/types/neg.asciidoc

@@ -1,8 +1,9 @@
 [%header.monospaced.styled,format=dsv,separator=|]
 |===
 v | result
+date_period | date_period
 double | double
-integer | double
-long | double
-unsigned_long | double
+integer | integer
+long | long
+time_duration | time_duration
 |===

+ 5 - 0
docs/reference/esql/functions/types/not_equals.asciidoc

@@ -0,0 +1,5 @@
+[%header.monospaced.styled,format=dsv,separator=|]
+|===
+lhs | rhs | result
+integer | integer | boolean
+|===

+ 11 - 0
docs/reference/esql/functions/types/sub.asciidoc

@@ -0,0 +1,11 @@
+[%header.monospaced.styled,format=dsv,separator=|]
+|===
+lhs | rhs | result
+date_period | date_period | date_period
+datetime | date_period | datetime
+datetime | time_duration | datetime
+double | double | double
+integer | integer | integer
+long | long | long
+time_duration | time_duration | time_duration
+|===

+ 5 - 1
docs/reference/esql/functions/unary.asciidoc

@@ -2,7 +2,11 @@
 [[esql-unary-operators]]
 === Unary operators
 
-These unary mathematical operators are supported:
+The only unary operators is negation (`-`):
 
 [.text-center]
 image::esql/functions/signature/neg.svg[Embedded,opts=inline]
+
+Supported types:
+
+include::types/neg.asciidoc[]

+ 18 - 8
x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/AbstractFunctionTestCase.java

@@ -993,23 +993,33 @@ public abstract class AbstractFunctionTestCase extends ESTestCase {
         if (System.getProperty("generateDocs") == null) {
             return;
         }
-        String name = functionName(); // TODO types table for operators
+        String name = functionName();
+        if (binaryOperator(name) != null) {
+            renderTypesTable(List.of("lhs", "rhs"));
+            return;
+        }
+        if (unaryOperator(name) != null) {
+            renderTypesTable(List.of("v"));
+            return;
+        }
         FunctionDefinition definition = definition(name);
-        if (definition == null) {
-            LogManager.getLogger(getTestClass()).info("Skipping rendering types because the function isn't registered");
+        if (definition != null) {
+            renderTypesTable(EsqlFunctionRegistry.description(definition).argNames());
             return;
         }
+        LogManager.getLogger(getTestClass()).info("Skipping rendering types because the function isn't registered");
+    }
 
-        List<String> args = EsqlFunctionRegistry.description(definition).argNames();
+    private static void renderTypesTable(List<String> argNames) throws IOException {
         StringBuilder header = new StringBuilder();
-        for (String arg : args) {
+        for (String arg : argNames) {
             header.append(arg).append(" | ");
         }
         header.append("result");
 
         List<String> table = new ArrayList<>();
         for (Map.Entry<List<DataType>, DataType> sig : signatures.entrySet()) {
-            if (sig.getKey().size() != args.size()) {
+            if (sig.getKey().size() != argNames.size()) {
                 continue;
             }
             StringBuilder b = new StringBuilder();
@@ -1052,9 +1062,9 @@ public abstract class AbstractFunctionTestCase extends ESTestCase {
             case "div" -> "/";
             case "equals" -> "==";
             case "greater_than" -> ">";
-            case "greater_than_or_equal_to" -> ">=";
+            case "greater_than_or_equal" -> ">=";
             case "less_than" -> "<";
-            case "less_than_or_equal_to" -> "<=";
+            case "less_than_or_equal" -> "<=";
             case "mod" -> "%";
             case "mul" -> "*";
             case "not_equals" -> "!=";