-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Sort Operator for Window Functions (#13619)
* Addition of NaiveSortMaker and Default implementation Add the NaiveSortMaker which makes a sorter object and a default implementation of the interface. This also allows us to plan multiple different window definitions on the same query.
- Loading branch information
1 parent
4ee4d99
commit f1821a7
Showing
33 changed files
with
2,550 additions
and
175 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
109 changes: 109 additions & 0 deletions
109
processing/src/main/java/org/apache/druid/query/operator/ColumnWithDirection.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,109 @@ | ||
/* | ||
* 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; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
import java.util.Objects; | ||
|
||
public class ColumnWithDirection | ||
{ | ||
public static ColumnWithDirection ascending(String column) | ||
{ | ||
return new ColumnWithDirection(column, Direction.ASC); | ||
} | ||
|
||
public static ColumnWithDirection descending(String column) | ||
{ | ||
return new ColumnWithDirection(column, Direction.DESC); | ||
} | ||
|
||
public enum Direction | ||
{ | ||
ASC(1), | ||
DESC(-1); | ||
|
||
private final int directionInt; | ||
|
||
Direction(int directionInt) | ||
{ | ||
this.directionInt = directionInt; | ||
} | ||
|
||
public int getDirectionInt() | ||
{ | ||
return directionInt; | ||
} | ||
} | ||
|
||
private final String columnName; | ||
private final Direction direction; | ||
|
||
@JsonCreator | ||
public ColumnWithDirection( | ||
@JsonProperty("column") String columnName, | ||
@JsonProperty("direction") Direction direction | ||
) | ||
{ | ||
this.columnName = columnName; | ||
this.direction = direction; | ||
} | ||
|
||
@JsonProperty("column") | ||
public String getColumn() | ||
{ | ||
return columnName; | ||
} | ||
|
||
@JsonProperty("direction") | ||
public Direction getDirection() | ||
{ | ||
return direction; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) | ||
{ | ||
if (this == o) { | ||
return true; | ||
} | ||
if (!(o instanceof ColumnWithDirection)) { | ||
return false; | ||
} | ||
ColumnWithDirection that = (ColumnWithDirection) o; | ||
return Objects.equals(columnName, that.columnName) && direction == that.direction; | ||
} | ||
|
||
@Override | ||
public int hashCode() | ||
{ | ||
return Objects.hash(columnName, direction); | ||
} | ||
|
||
@Override | ||
public String toString() | ||
{ | ||
return "ColumnWithDirection{" + | ||
"columnName='" + columnName + '\'' + | ||
", direction=" + direction + | ||
'}'; | ||
} | ||
} |
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
74 changes: 74 additions & 0 deletions
74
processing/src/main/java/org/apache/druid/query/operator/NaiveSortOperator.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,74 @@ | ||
/* | ||
* 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; | ||
|
||
import org.apache.druid.query.rowsandcols.RowsAndColumns; | ||
import org.apache.druid.query.rowsandcols.semantic.NaiveSortMaker; | ||
|
||
import java.util.ArrayList; | ||
|
||
/** | ||
* A naive sort operator is an operation that sorts a stream of data in-place. Generally speaking this means | ||
* that it has to accumulate all of the data of its child operator first before it can sort. This limitation | ||
* means that hopefully this operator is only planned in a very small number of circumstances. | ||
*/ | ||
public class NaiveSortOperator implements Operator | ||
{ | ||
private final Operator child; | ||
private final ArrayList<ColumnWithDirection> sortColumns; | ||
|
||
public NaiveSortOperator( | ||
Operator child, | ||
ArrayList<ColumnWithDirection> sortColumns | ||
) | ||
{ | ||
this.child = child; | ||
this.sortColumns = sortColumns; | ||
} | ||
|
||
@Override | ||
public void go(Receiver receiver) | ||
{ | ||
child.go( | ||
new Receiver() | ||
{ | ||
NaiveSortMaker.NaiveSorter sorter = null; | ||
|
||
@Override | ||
public boolean push(RowsAndColumns rac) | ||
{ | ||
if (sorter == null) { | ||
sorter = NaiveSortMaker.fromRAC(rac).make(sortColumns); | ||
} else { | ||
sorter.moreData(rac); | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
public void completed() | ||
{ | ||
receiver.push(sorter.complete()); | ||
receiver.completed(); | ||
} | ||
} | ||
); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
processing/src/main/java/org/apache/druid/query/operator/NaiveSortOperatorFactory.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,59 @@ | ||
/* | ||
* 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; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
import java.util.ArrayList; | ||
|
||
public class NaiveSortOperatorFactory implements OperatorFactory | ||
{ | ||
private final ArrayList<ColumnWithDirection> sortColumns; | ||
|
||
@JsonCreator | ||
public NaiveSortOperatorFactory( | ||
@JsonProperty("columns") ArrayList<ColumnWithDirection> sortColumns | ||
) | ||
{ | ||
this.sortColumns = sortColumns; | ||
} | ||
|
||
@JsonProperty("columns") | ||
public ArrayList<ColumnWithDirection> getSortColumns() | ||
{ | ||
return sortColumns; | ||
} | ||
|
||
@Override | ||
public Operator wrap(Operator op) | ||
{ | ||
return new NaiveSortOperator(op, sortColumns); | ||
} | ||
|
||
@Override | ||
public boolean validateEquivalent(OperatorFactory other) | ||
{ | ||
if (other instanceof NaiveSortOperatorFactory) { | ||
return sortColumns.equals(((NaiveSortOperatorFactory) other).getSortColumns()); | ||
} | ||
return false; | ||
} | ||
} |
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
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.