Repository navigation
Support Framing for Window Aggregations #13514
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.apache.druid.query.operator.window; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonCreator; | ||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| public class WindowFrame | ||
| { | ||
| @SuppressWarnings("unused") | ||
| public enum PeerType | ||
|
Member
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 is worth a javadoc, it is described in some other places, such as
Contributor
Author
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. Yeah, word. This is a SQL concept stolen from... SQL, so I didn't think to document it as much as I probably should have. When we add support for RANGE peers, we can remove the suppression and actually document it. |
||
| { | ||
| ROWS, | ||
| RANGE | ||
| } | ||
|
|
||
| // Will likely need to add the order by columns to also be able to deal with RANGE peer type. | ||
| private final PeerType peerType; | ||
| private final boolean lowerUnbounded; | ||
| private final int lowerOffset; | ||
| private final boolean upperUnbounded; | ||
| private final int upperOffset; | ||
|
|
||
| @JsonCreator | ||
| public WindowFrame( | ||
| @JsonProperty("peerType") PeerType peerType, | ||
| @JsonProperty("lowUnbounded") boolean lowerUnbounded, | ||
| @JsonProperty("lowOffset") int lowerOffset, | ||
| @JsonProperty("uppUnbounded") boolean upperUnbounded, | ||
| @JsonProperty("uppOffset") int upperOffset | ||
| ) | ||
| { | ||
| this.peerType = peerType; | ||
| this.lowerUnbounded = lowerUnbounded; | ||
| this.lowerOffset = lowerOffset; | ||
| this.upperUnbounded = upperUnbounded; | ||
| this.upperOffset = upperOffset; | ||
| } | ||
|
|
||
| @JsonProperty("peerType") | ||
| public PeerType getPeerType() | ||
| { | ||
| return peerType; | ||
| } | ||
|
|
||
| @JsonProperty("lowUnbounded") | ||
| public boolean isLowerUnbounded() | ||
| { | ||
| return lowerUnbounded; | ||
| } | ||
|
|
||
| @JsonProperty("lowOffset") | ||
| public int getLowerOffset() | ||
| { | ||
| return lowerOffset; | ||
| } | ||
|
|
||
| @JsonProperty("uppUnbounded") | ||
| public boolean isUpperUnbounded() | ||
| { | ||
| return upperUnbounded; | ||
| } | ||
|
|
||
| @JsonProperty("uppOffset") | ||
| public int getUpperOffset() | ||
| { | ||
| return upperOffset; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) | ||
| { | ||
| if (this == o) { | ||
| return true; | ||
| } | ||
| if (!(o instanceof WindowFrame)) { | ||
| return false; | ||
| } | ||
| WindowFrame that = (WindowFrame) o; | ||
| return lowerUnbounded == that.lowerUnbounded | ||
| && lowerOffset == that.lowerOffset | ||
| && upperUnbounded == that.upperUnbounded | ||
| && upperOffset == that.upperOffset | ||
| && peerType == that.peerType; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() | ||
| { | ||
| return Objects.hash(peerType, lowerUnbounded, lowerOffset, upperUnbounded, upperOffset); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() | ||
| { | ||
| return "WindowFrame{" + | ||
| "peerType=" + peerType + | ||
| ", lowerUnbounded=" + lowerUnbounded + | ||
| ", lowerOffset=" + lowerOffset + | ||
| ", upperUnbounded=" + upperUnbounded + | ||
| ", upperOffset=" + upperOffset + | ||
| '}'; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.apache.druid.query.operator.window; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonCreator; | ||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||
| import org.apache.druid.query.aggregation.AggregatorFactory; | ||
| import org.apache.druid.query.rowsandcols.RowsAndColumns; | ||
| import org.apache.druid.query.rowsandcols.semantic.DefaultFramedOnHeapAggregatable; | ||
| import org.apache.druid.query.rowsandcols.semantic.FramedOnHeapAggregatable; | ||
|
|
||
| import javax.annotation.Nullable; | ||
| import java.util.Arrays; | ||
|
|
||
| public class WindowFramedAggregateProcessor implements Processor | ||
| { | ||
| @Nullable | ||
| private static <T> T[] emptyToNull(T[] arr) | ||
| { | ||
| if (arr == null || arr.length == 0) { | ||
| return null; | ||
| } else { | ||
| return arr; | ||
| } | ||
| } | ||
|
|
||
| private final WindowFrame frame; | ||
| private final AggregatorFactory[] aggregations; | ||
|
|
||
| @JsonCreator | ||
| public WindowFramedAggregateProcessor( | ||
| @JsonProperty("frame") WindowFrame frame, | ||
| @JsonProperty("aggregations") AggregatorFactory[] aggregations | ||
| ) | ||
| { | ||
| this.frame = frame; | ||
| this.aggregations = emptyToNull(aggregations); | ||
| } | ||
|
|
||
| @JsonProperty("frame") | ||
| public WindowFrame getFrame() | ||
| { | ||
| return frame; | ||
| } | ||
|
|
||
| @JsonProperty("aggregations") | ||
| public AggregatorFactory[] getAggregations() | ||
| { | ||
| return aggregations; | ||
| } | ||
|
|
||
| @Override | ||
| public RowsAndColumns process(RowsAndColumns inputPartition) | ||
| { | ||
| FramedOnHeapAggregatable agger = inputPartition.as(FramedOnHeapAggregatable.class); | ||
| if (agger == null) { | ||
| agger = new DefaultFramedOnHeapAggregatable(RowsAndColumns.expectAppendable(inputPartition)); | ||
| } | ||
|
|
||
| return agger.aggregateAll(frame, aggregations); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean validateEquivalent(Processor otherProcessor) | ||
| { | ||
| if (otherProcessor instanceof WindowFramedAggregateProcessor) { | ||
| WindowFramedAggregateProcessor other = (WindowFramedAggregateProcessor) otherProcessor; | ||
| return frame.equals(other.frame) && Arrays.equals(aggregations, other.aggregations); | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
|
|
||
| @Override | ||
| public String toString() | ||
| { | ||
| return "WindowFramedAggregateProcessor{" + | ||
| "frame=" + frame + | ||
| ", aggregations=" + Arrays.toString(aggregations) + | ||
| '}'; | ||
| } | ||
| } |
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.
heh, its a shame that "frame" really is the best name this; hopefully it wont be too confusing since it doesn't really have anything to do with the other one, https://github.com/apache/druid/blob/master/processing/src/main/java/org/apache/druid/frame/Frame.java which should maybe be called DataFrame or... idk naming is just the worst 😅
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.
DataFrame is a thing in R too... I don't think it's far from what a
org.apache.druid.frame.Frameis though, so maybe that's actually a good name?