Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* ES|QL: Add unit tests for now() function (#108498) * Fix for bwc
- Loading branch information
1 parent
9341c15
commit 17dff75
Showing
12 changed files
with
157 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] | ||
|=== | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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[] |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// This is generated by ESQL's AbstractFunctionTestCase. Do no edit it. See ../README.md for how to regenerate it. | ||
|
||
*Parameters* |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|=== |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
.../src/test/java/org/elasticsearch/xpack/esql/expression/function/scalar/math/NowTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* 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.AbstractFunctionTestCase; | ||
import org.elasticsearch.xpack.esql.expression.function.TestCaseSupplier; | ||
import org.elasticsearch.xpack.esql.expression.function.scalar.date.Now; | ||
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 AbstractFunctionTestCase { | ||
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(new TestCaseSupplier.TypedData(1, DataTypes.INTEGER, "foo")), | ||
matchesPattern("LiteralsEvaluator\\[lit=.*\\]"), | ||
DataTypes.DATETIME, | ||
equalTo(EsqlTestUtils.TEST_CFG.now().toInstant().toEpochMilli()) | ||
) | ||
) | ||
) | ||
); | ||
} | ||
|
||
@Override | ||
protected Expression build(Source source, List<Expression> args) { | ||
return new Now(Source.EMPTY, EsqlTestUtils.TEST_CFG); | ||
} | ||
|
||
@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()); | ||
} | ||
|
||
} |