-
Notifications
You must be signed in to change notification settings - Fork 24.9k
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
[ML-DataFrame] Add support for (date) histogram pivots #38725
Merged
benwtrent
merged 4 commits into
elastic:feature/fib
from
benwtrent:feature/ml-dataframe-add-histo-pivot
Feb 11, 2019
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2a1e95d
[FEATURE][DATA_FRAME] Adding (date) histogram group_by support for pivot
benwtrent 454fc9e
Merge remote-tracking branch 'upstream/feature/fib' into feature/ml-d…
benwtrent ead6cd3
adjusting format for merge
benwtrent 18b36ab
Update DataFramePivotRestIT.java
benwtrent 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 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
177 changes: 177 additions & 0 deletions
177
...ain/java/org/elasticsearch/xpack/dataframe/transforms/pivot/DateHistogramGroupSource.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,177 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
package org.elasticsearch.xpack.dataframe.transforms.pivot; | ||
|
||
import org.elasticsearch.common.ParseField; | ||
import org.elasticsearch.common.io.stream.StreamInput; | ||
import org.elasticsearch.common.io.stream.StreamOutput; | ||
import org.elasticsearch.common.xcontent.ConstructingObjectParser; | ||
import org.elasticsearch.common.xcontent.ObjectParser; | ||
import org.elasticsearch.common.xcontent.XContentBuilder; | ||
import org.elasticsearch.common.xcontent.XContentParser; | ||
import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval; | ||
|
||
import java.io.IOException; | ||
import java.time.ZoneId; | ||
import java.time.ZoneOffset; | ||
import java.util.Objects; | ||
|
||
|
||
public class DateHistogramGroupSource extends SingleGroupSource<DateHistogramGroupSource> { | ||
|
||
private static final String NAME = "data_frame_date_histogram_group"; | ||
private static final ParseField TIME_ZONE = new ParseField("time_zone"); | ||
private static final ParseField FORMAT = new ParseField("format"); | ||
|
||
private static final ConstructingObjectParser<DateHistogramGroupSource, Void> STRICT_PARSER = createParser(false); | ||
private static final ConstructingObjectParser<DateHistogramGroupSource, Void> LENIENT_PARSER = createParser(true); | ||
private long interval = 0; | ||
private DateHistogramInterval dateHistogramInterval; | ||
private String format; | ||
private ZoneId timeZone; | ||
|
||
public DateHistogramGroupSource(String field) { | ||
super(field); | ||
} | ||
|
||
public DateHistogramGroupSource(StreamInput in) throws IOException { | ||
super(in); | ||
this.interval = in.readLong(); | ||
this.dateHistogramInterval = in.readOptionalWriteable(DateHistogramInterval::new); | ||
this.timeZone = in.readOptionalZoneId(); | ||
this.format = in.readOptionalString(); | ||
} | ||
|
||
private static ConstructingObjectParser<DateHistogramGroupSource, Void> createParser(boolean lenient) { | ||
ConstructingObjectParser<DateHistogramGroupSource, Void> parser = new ConstructingObjectParser<>(NAME, lenient, (args) -> { | ||
String field = (String) args[0]; | ||
return new DateHistogramGroupSource(field); | ||
}); | ||
|
||
SingleGroupSource.declareValuesSourceFields(parser, null); | ||
|
||
parser.declareField((histogram, interval) -> { | ||
if (interval instanceof Long) { | ||
histogram.setInterval((long) interval); | ||
} else { | ||
histogram.setDateHistogramInterval((DateHistogramInterval) interval); | ||
} | ||
}, p -> { | ||
if (p.currentToken() == XContentParser.Token.VALUE_NUMBER) { | ||
return p.longValue(); | ||
} else { | ||
return new DateHistogramInterval(p.text()); | ||
} | ||
}, HistogramGroupSource.INTERVAL, ObjectParser.ValueType.LONG); | ||
|
||
parser.declareField(DateHistogramGroupSource::setTimeZone, p -> { | ||
if (p.currentToken() == XContentParser.Token.VALUE_STRING) { | ||
return ZoneId.of(p.text()); | ||
} else { | ||
return ZoneOffset.ofHours(p.intValue()); | ||
} | ||
}, TIME_ZONE, ObjectParser.ValueType.LONG); | ||
|
||
parser.declareString(DateHistogramGroupSource::setFormat, FORMAT); | ||
return parser; | ||
} | ||
|
||
public static DateHistogramGroupSource fromXContent(final XContentParser parser, boolean lenient) throws IOException { | ||
return lenient ? LENIENT_PARSER.apply(parser, null) : STRICT_PARSER.apply(parser, null); | ||
} | ||
|
||
public long getInterval() { | ||
return interval; | ||
} | ||
|
||
public void setInterval(long interval) { | ||
if (interval < 1) { | ||
throw new IllegalArgumentException("[interval] must be greater than or equal to 1."); | ||
} | ||
this.interval = interval; | ||
} | ||
|
||
public DateHistogramInterval getDateHistogramInterval() { | ||
return dateHistogramInterval; | ||
} | ||
|
||
public void setDateHistogramInterval(DateHistogramInterval dateHistogramInterval) { | ||
if (dateHistogramInterval == null) { | ||
throw new IllegalArgumentException("[dateHistogramInterval] must not be null"); | ||
} | ||
this.dateHistogramInterval = dateHistogramInterval; | ||
} | ||
|
||
public String getFormat() { | ||
return format; | ||
} | ||
|
||
public void setFormat(String format) { | ||
this.format = format; | ||
} | ||
|
||
public ZoneId getTimeZone() { | ||
return timeZone; | ||
} | ||
|
||
public void setTimeZone(ZoneId timeZone) { | ||
this.timeZone = timeZone; | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
out.writeOptionalString(field); | ||
out.writeLong(interval); | ||
out.writeOptionalWriteable(dateHistogramInterval); | ||
out.writeOptionalZoneId(timeZone); | ||
out.writeOptionalString(format); | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
builder.startObject(); | ||
if (field != null) { | ||
builder.field(FIELD.getPreferredName(), field); | ||
} | ||
if (dateHistogramInterval == null) { | ||
builder.field(HistogramGroupSource.INTERVAL.getPreferredName(), interval); | ||
} else { | ||
builder.field(HistogramGroupSource.INTERVAL.getPreferredName(), dateHistogramInterval.toString()); | ||
} | ||
if (timeZone != null) { | ||
builder.field(TIME_ZONE.getPreferredName(), timeZone.toString()); | ||
} | ||
if (format != null) { | ||
builder.field(FORMAT.getPreferredName(), format); | ||
} | ||
builder.endObject(); | ||
return builder; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (this == other) { | ||
return true; | ||
} | ||
|
||
if (other == null || getClass() != other.getClass()) { | ||
return false; | ||
} | ||
|
||
final DateHistogramGroupSource that = (DateHistogramGroupSource) other; | ||
|
||
return Objects.equals(this.field, that.field) && | ||
Objects.equals(interval, that.interval) && | ||
Objects.equals(dateHistogramInterval, that.dateHistogramInterval) && | ||
Objects.equals(timeZone, that.timeZone) && | ||
Objects.equals(format, that.format); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(field, interval, dateHistogramInterval, timeZone, format); | ||
} | ||
} |
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
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.
nit: comment says 17