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
SQL: Fix metric aggs on date/time to not return double #40377
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -18,8 +18,10 @@ | |
import org.elasticsearch.search.aggregations.metrics.Percentiles; | ||
import org.elasticsearch.xpack.sql.SqlIllegalArgumentException; | ||
import org.elasticsearch.xpack.sql.querydsl.agg.Aggs; | ||
import org.elasticsearch.xpack.sql.util.DateUtils; | ||
|
||
import java.io.IOException; | ||
import java.time.ZoneId; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
|
@@ -30,24 +32,32 @@ public class MetricAggExtractor implements BucketExtractor { | |
private final String name; | ||
private final String property; | ||
private final String innerKey; | ||
private final boolean isDateTimeBased; | ||
private final ZoneId zoneId; | ||
|
||
public MetricAggExtractor(String name, String property, String innerKey) { | ||
public MetricAggExtractor(String name, String property, String innerKey, ZoneId zoneId, boolean isDateTimeBased) { | ||
this.name = name; | ||
this.property = property; | ||
this.innerKey = innerKey; | ||
this. isDateTimeBased =isDateTimeBased; | ||
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. Weird spacing. |
||
this.zoneId = zoneId; | ||
} | ||
|
||
MetricAggExtractor(StreamInput in) throws IOException { | ||
name = in.readString(); | ||
property = in.readString(); | ||
innerKey = in.readOptionalString(); | ||
isDateTimeBased = in.readBoolean(); | ||
zoneId = ZoneId.of(in.readString()); | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
out.writeString(name); | ||
out.writeString(property); | ||
out.writeOptionalString(innerKey); | ||
out.writeBoolean(isDateTimeBased); | ||
out.writeString(zoneId.getId()); | ||
} | ||
|
||
String name() { | ||
|
@@ -62,6 +72,10 @@ String innerKey() { | |
return innerKey; | ||
} | ||
|
||
ZoneId zoneId() { | ||
return zoneId; | ||
} | ||
|
||
@Override | ||
public String getWriteableName() { | ||
return NAME; | ||
|
@@ -83,20 +97,33 @@ public Object extract(Bucket bucket) { | |
//if (innerKey == null) { | ||
// throw new SqlIllegalArgumentException("Invalid innerKey {} specified for aggregation {}", innerKey, name); | ||
//} | ||
return ((InternalNumericMetricsAggregation.MultiValue) agg).value(property); | ||
return handleDateTime(((InternalNumericMetricsAggregation.MultiValue) agg).value(property)); | ||
} else if (agg instanceof InternalFilter) { | ||
// COUNT(expr) and COUNT(ALL expr) uses this type of aggregation to account for non-null values only | ||
return ((InternalFilter) agg).getDocCount(); | ||
} | ||
|
||
Object v = agg.getProperty(property); | ||
return innerKey != null && v instanceof Map ? ((Map<?, ?>) v).get(innerKey) : v; | ||
return handleDateTime(innerKey != null && v instanceof Map ? ((Map<?, ?>) v).get(innerKey) : v); | ||
} | ||
|
||
private Object handleDateTime(Object object) { | ||
if (isDateTimeBased) { | ||
if (object == null) { | ||
return object; | ||
} else if (object instanceof Number) { | ||
return DateUtils.asDateTime(((Number) object).longValue(), zoneId); | ||
} else { | ||
throw new SqlIllegalArgumentException("Invalid date key returned: {}", object); | ||
} | ||
} | ||
return object; | ||
} | ||
|
||
/** | ||
* Check if the given aggregate has been executed and has computed values | ||
* or not (the bucket is null). | ||
* | ||
* | ||
* Waiting on https://github.com/elastic/elasticsearch/issues/34903 | ||
*/ | ||
private static boolean containsValues(InternalAggregation agg) { | ||
|
@@ -130,11 +157,11 @@ public boolean equals(Object obj) { | |
if (this == obj) { | ||
return true; | ||
} | ||
|
||
if (obj == null || getClass() != obj.getClass()) { | ||
return false; | ||
} | ||
|
||
MetricAggExtractor other = (MetricAggExtractor) obj; | ||
return Objects.equals(name, other.name) | ||
&& Objects.equals(property, other.property) | ||
|
@@ -146,4 +173,4 @@ public String toString() { | |
String i = innerKey != null ? "[" + innerKey + "]" : ""; | ||
return Aggs.ROOT_GROUP_NAME + ">" + name + "." + property + i; | ||
} | ||
} | ||
} |
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
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
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.
If this test here wasn't complaining before, this means that our SQL spec test case infrastructure isn't actually comparing an actual date type of data, right? Otherwise, the test would have failed.
If so, I would like to see a test with
MIN(date_field)
in CSV spec test case infra, if possible. I have seen the unit test that compares the metric agg key with an actual date (assertEquals(DateUtils.asDateTime((long) innerValue , zoneId), extractor.extract(bucket));
) but I believe our tests would be more complete if there would be an integration test as well (CSV-spec in this case).