فهرست منبع

ES|QL: Add unit tests for now() function (#108498)

Luigi Dell'Aquila 1 سال پیش
والد
کامیت
fed808850d

+ 1 - 1
docs/reference/esql/functions/date-time-functions.asciidoc

@@ -21,4 +21,4 @@ include::layout/date_extract.asciidoc[]
 include::layout/date_format.asciidoc[]
 include::layout/date_parse.asciidoc[]
 include::layout/date_trunc.asciidoc[]
-include::now.asciidoc[]
+include::layout/now.asciidoc[]

+ 5 - 0
docs/reference/esql/functions/description/now.asciidoc

@@ -0,0 +1,5 @@
+// This is generated by ESQL's AbstractFunctionTestCase. Do no edit it. See ../README.md for how to regenerate it.
+
+*Description*
+
+Returns current date and time.

+ 22 - 0
docs/reference/esql/functions/examples/now.asciidoc

@@ -0,0 +1,22 @@
+// This is generated by ESQL's AbstractFunctionTestCase. Do no edit it. See ../README.md for how to regenerate it.
+
+*Examples*
+
+[source.merge.styled,esql]
+----
+include::{esql-specs}/date.csv-spec[tag=docsNow]
+----
+[%header.monospaced.styled,format=dsv,separator=|]
+|===
+include::{esql-specs}/date.csv-spec[tag=docsNow-result]
+|===
+To retrieve logs from the last hour:
+[source.merge.styled,esql]
+----
+include::{esql-specs}/date.csv-spec[tag=docsNowWhere]
+----
+[%header.monospaced.styled,format=dsv,separator=|]
+|===
+include::{esql-specs}/date.csv-spec[tag=docsNowWhere-result]
+|===
+

+ 16 - 0
docs/reference/esql/functions/kibana/definition/now.json

@@ -0,0 +1,16 @@
+{
+  "comment" : "This is generated by ESQL's AbstractFunctionTestCase. Do no edit it. See ../README.md for how to regenerate it.",
+  "type" : "eval",
+  "name" : "now",
+  "description" : "Returns current date and time.",
+  "signatures" : [
+    {
+      "params" : [ ],
+      "returnType" : "datetime"
+    }
+  ],
+  "examples" : [
+    "ROW current_date = NOW()",
+    "FROM sample_data\n| WHERE @timestamp > NOW() - 1 hour"
+  ]
+}

+ 10 - 0
docs/reference/esql/functions/kibana/docs/now.md

@@ -0,0 +1,10 @@
+<!--
+This is generated by ESQL's AbstractFunctionTestCase. Do no edit it. See ../README.md for how to regenerate it.
+-->
+
+### NOW
+Returns current date and time.
+
+```
+ROW current_date = NOW()
+```

+ 15 - 0
docs/reference/esql/functions/layout/now.asciidoc

@@ -0,0 +1,15 @@
+// This is generated by ESQL's AbstractFunctionTestCase. Do no edit it. See ../README.md for how to regenerate it.
+
+[discrete]
+[[esql-now]]
+=== `NOW`
+
+*Syntax*
+
+[.text-center]
+image::esql/functions/signature/now.svg[Embedded,opts=inline]
+
+include::../parameters/now.asciidoc[]
+include::../description/now.asciidoc[]
+include::../types/now.asciidoc[]
+include::../examples/now.asciidoc[]

+ 0 - 28
docs/reference/esql/functions/now.asciidoc

@@ -1,28 +0,0 @@
-[discrete]
-[[esql-now]]
-=== `NOW`
-
-*Syntax*
-
-[source,esql]
-----
-NOW()
-----
-
-*Description*
-
-Returns current date and time.
-
-*Example*
-
-[source,esql]
-----
-include::{esql-specs}/date.csv-spec[tag=docsNow]
-----
-
-To retrieve logs from the last hour:
-
-[source,esql]
-----
-include::{esql-specs}/date.csv-spec[tag=docsNowWhere]
-----

+ 3 - 0
docs/reference/esql/functions/parameters/now.asciidoc

@@ -0,0 +1,3 @@
+// This is generated by ESQL's AbstractFunctionTestCase. Do no edit it. See ../README.md for how to regenerate it.
+
+*Parameters*

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

@@ -0,0 +1 @@
+<svg version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg" width="150" height="46" viewbox="0 0 150 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 31h5m56 0h10m32 0h10m32 0h5"/><rect class="s" x="5" y="5" width="56" height="36"/><text class="k" x="15" y="31">NOW</text><rect class="s" x="71" y="5" width="32" height="36" rx="7"/><text class="syn" x="81" y="31">(</text><rect class="s" x="113" y="5" width="32" height="36" rx="7"/><text class="syn" x="123" y="31">)</text></svg>

+ 9 - 0
docs/reference/esql/functions/types/now.asciidoc

@@ -0,0 +1,9 @@
+// This is generated by ESQL's AbstractFunctionTestCase. Do no edit it. See ../README.md for how to regenerate it.
+
+*Supported types*
+
+[%header.monospaced.styled,format=dsv,separator=|]
+|===
+result
+datetime
+|===

+ 68 - 0
x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/scalar/math/NowTests.java

@@ -0,0 +1,68 @@
+/*
+ * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
+ * or more contributor license agreements. Licensed under the Elastic License
+ * 2.0; you may not use this file except in compliance with the Elastic License
+ * 2.0.
+ */
+
+package org.elasticsearch.xpack.esql.expression.function.scalar.math;
+
+import com.carrotsearch.randomizedtesting.annotations.Name;
+import com.carrotsearch.randomizedtesting.annotations.ParametersFactory;
+
+import org.elasticsearch.compute.data.Block;
+import org.elasticsearch.compute.data.LongBlock;
+import org.elasticsearch.xpack.esql.EsqlTestUtils;
+import org.elasticsearch.xpack.esql.expression.function.TestCaseSupplier;
+import org.elasticsearch.xpack.esql.expression.function.scalar.AbstractConfigurationFunctionTestCase;
+import org.elasticsearch.xpack.esql.expression.function.scalar.date.Now;
+import org.elasticsearch.xpack.esql.session.EsqlConfiguration;
+import org.elasticsearch.xpack.ql.expression.Expression;
+import org.elasticsearch.xpack.ql.tree.Source;
+import org.elasticsearch.xpack.ql.type.DataTypes;
+import org.hamcrest.Matcher;
+
+import java.util.List;
+import java.util.function.Supplier;
+
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.matchesPattern;
+
+public class NowTests extends AbstractConfigurationFunctionTestCase {
+    public NowTests(@Name("TestCase") Supplier<TestCaseSupplier.TestCase> testCaseSupplier) {
+        this.testCase = testCaseSupplier.get();
+    }
+
+    @ParametersFactory
+    public static Iterable<Object[]> parameters() {
+        return parameterSuppliersFromTypedData(
+            List.of(
+                new TestCaseSupplier(
+                    "Now Test",
+                    () -> new TestCaseSupplier.TestCase(
+                        List.of(),
+                        matchesPattern("LiteralsEvaluator\\[lit=.*\\]"),
+                        DataTypes.DATETIME,
+                        equalTo(EsqlTestUtils.TEST_CFG.now().toInstant().toEpochMilli())
+                    )
+                )
+            )
+        );
+    }
+
+    @Override
+    protected Expression buildWithConfiguration(Source source, List<Expression> args, EsqlConfiguration configuration) {
+        return new Now(Source.EMPTY, configuration);
+    }
+
+    @Override
+    protected void assertSimpleWithNulls(List<Object> data, Block value, int nullBlock) {
+        assertThat(((LongBlock) value).asVector().getLong(0), equalTo(EsqlTestUtils.TEST_CFG.now().toInstant().toEpochMilli()));
+    }
+
+    @Override
+    protected Matcher<Object> allNullsMatcher() {
+        return equalTo(EsqlTestUtils.TEST_CFG.now().toInstant().toEpochMilli());
+    }
+
+}