-
Notifications
You must be signed in to change notification settings - Fork 25.6k
ES|QL: add tests and fix docs for LOOKUP JOIN with index datemath #130535
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
luigidellaquila
merged 3 commits into
elastic:main
from
luigidellaquila:esql/index_datemath_tests
Jul 3, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -40,6 +40,9 @@ | |
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.time.ZoneOffset; | ||
| import java.time.ZonedDateTime; | ||
| import java.time.format.DateTimeFormatter; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.Comparator; | ||
|
|
@@ -786,6 +789,117 @@ && isMillisOrNanos(listOfTypes.get(j))) { | |
| } | ||
| } | ||
|
|
||
| public void testDateMathIndexPattern() throws IOException { | ||
| ZonedDateTime now = ZonedDateTime.now(ZoneOffset.UTC); | ||
|
|
||
| String[] indices = { | ||
| "test-index-" + DateTimeFormatter.ofPattern("yyyy", Locale.ROOT).format(now), | ||
| "test-index-" + DateTimeFormatter.ofPattern("yyyy", Locale.ROOT).format(now.minusYears(1)), | ||
| "test-index-" + DateTimeFormatter.ofPattern("yyyy", Locale.ROOT).format(now.minusYears(2)) }; | ||
|
|
||
| int idx = 0; | ||
| for (String index : indices) { | ||
| createIndex(index); | ||
| for (int i = 0; i < 10; i++) { | ||
| Request request = new Request("POST", "/" + index + "/_doc/"); | ||
| request.addParameter("refresh", "true"); | ||
| request.setJsonEntity("{\"f\":" + idx++ + "}"); | ||
| assertOK(client().performRequest(request)); | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this might need a refresh. |
||
| } | ||
|
|
||
| String query = """ | ||
| { | ||
| "query": "from <test-index-{now/d{yyyy}}> | sort f asc | limit 1 | keep f" | ||
| } | ||
| """; | ||
| Request request = new Request("POST", "/_query"); | ||
| request.setJsonEntity(query); | ||
| Response resp = client().performRequest(request); | ||
| Map<String, Object> results = entityAsMap(resp); | ||
| List<?> values = (List<?>) results.get("values"); | ||
| assertThat(values.size(), is(1)); | ||
| List<?> row = (List<?>) values.get(0); | ||
| assertThat(row.get(0), is(0)); | ||
|
|
||
| query = """ | ||
| { | ||
| "query": "from <test-index-{now/d-1y{yyyy}}> | sort f asc | limit 1 | keep f" | ||
| } | ||
| """; | ||
| request = new Request("POST", "/_query"); | ||
| request.setJsonEntity(query); | ||
| resp = client().performRequest(request); | ||
| results = entityAsMap(resp); | ||
| values = (List<?>) results.get("values"); | ||
| assertThat(values.size(), is(1)); | ||
| row = (List<?>) values.get(0); | ||
| assertThat(row.get(0), is(10)); | ||
|
|
||
| for (String index : indices) { | ||
| assertThat(deleteIndex(index).isAcknowledged(), is(true)); // clean up | ||
| } | ||
| } | ||
|
|
||
| public void testDateMathInJoin() throws IOException { | ||
| ZonedDateTime now = ZonedDateTime.now(ZoneOffset.UTC); | ||
|
|
||
| createIndex("idx", Settings.EMPTY, """ | ||
| { | ||
| "properties": { | ||
| "key": { | ||
| "type": "keyword" | ||
| } | ||
| } | ||
| } | ||
| """); | ||
|
|
||
| Request request = new Request("POST", "/idx/_doc/"); | ||
| request.addParameter("refresh", "true"); | ||
| request.setJsonEntity("{\"key\":\"foo\"}"); | ||
| assertOK(client().performRequest(request)); | ||
|
|
||
| String[] lookupIndices = { | ||
| "lookup-index-" + DateTimeFormatter.ofPattern("yyyy", Locale.ROOT).format(now), | ||
| "lookup-index-" + DateTimeFormatter.ofPattern("yyyy", Locale.ROOT).format(now.minusYears(1)) }; | ||
|
|
||
| for (String index : lookupIndices) { | ||
| createIndex(index, Settings.builder().put("mode", "lookup").build(), """ | ||
| { | ||
| "properties": { | ||
| "key": { | ||
| "type": "keyword" | ||
| } | ||
| } | ||
| } | ||
| """); | ||
| request = new Request("POST", "/" + index + "/_doc/"); | ||
| request.addParameter("refresh", "true"); | ||
| request.setJsonEntity("{\"key\":\"foo\", \"value\": \"" + index + "\"}"); | ||
| assertOK(client().performRequest(request)); | ||
| } | ||
|
|
||
| String[] queries = { | ||
| "from idx | lookup join <lookup-index-{now/d{yyyy}}> on key | limit 1", | ||
| "from idx | lookup join <lookup-index-{now/d-1y{yyyy}}> on key | limit 1" }; | ||
| for (int i = 0; i < queries.length; i++) { | ||
| String queryPayload = "{\"query\": \"" + queries[i] + "\"}"; | ||
| request = new Request("POST", "/_query"); | ||
| request.setJsonEntity(queryPayload); | ||
| Response resp = client().performRequest(request); | ||
| Map<String, Object> results = entityAsMap(resp); | ||
| List<?> values = (List<?>) results.get("values"); | ||
| assertThat(values.size(), is(1)); | ||
| List<?> row = (List<?>) values.get(0); | ||
| assertThat(row.get(1), is(lookupIndices[i])); | ||
| } | ||
|
|
||
| assertThat(deleteIndex("idx").isAcknowledged(), is(true)); // clean up | ||
| for (String index : lookupIndices) { | ||
| assertThat(deleteIndex(index).isAcknowledged(), is(true)); // clean up | ||
| } | ||
| } | ||
|
|
||
| static MapMatcher commonProfile() { | ||
| return matchesMap() // | ||
| .entry("description", any(String.class)) | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using now may lead to flakiness when the tests are executed around midnight, no? Maybe a fixed date would be better?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought about it, but date math is all about dynamic patterns using
now. I'm not sure a test with a fixed date will be realistic.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One thing I can do is to use the year only, that should drastically reduce the risk of flakiness
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How long does this test runs?
I have seen some sql/eql examples that add assumption they are executed at least 10-15 minutes before midnight.