Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,25 @@ public void testSwapWindowFunctions() {
DATABASE_NAME);
}

@Test
public void testSameWindowFunctionWithDifferentOrdering() {
String[] expectedHeader = new String[] {"time", "device", "value", "rank_time", "rank_value"};
String[] retArray =
new String[] {
"2021-01-01T09:05:00.000Z,d1,3.0,1,2,",
"2021-01-01T09:07:00.000Z,d1,5.0,2,4,",
"2021-01-01T09:09:00.000Z,d1,3.0,3,2,",
"2021-01-01T09:10:00.000Z,d1,1.0,4,1,",
"2021-01-01T09:08:00.000Z,d2,2.0,1,1,",
"2021-01-01T09:15:00.000Z,d2,4.0,2,2,",
};
tableResultSetEqualTest(
"SELECT *, rank() OVER (PARTITION BY device ORDER BY \"time\") AS rank_time, rank() OVER (PARTITION BY device ORDER BY value) AS rank_value FROM demo ORDER BY device, \"time\"",
expectedHeader,
retArray,
DATABASE_NAME);
}

@Test
public void testPushDownFilterIntoWindow() {
String[] expectedHeader = new String[] {"time", "device", "value", "rn"};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,9 @@ private PlanBuilder planWindowFunctions(

Map<Analysis.ResolvedWindow, List<FunctionCall>> functions =
scopeAwareDistinct(subPlan, windowFunctions).stream()
.collect(Collectors.groupingBy(analysis::getWindow));
.collect(
Collectors.groupingBy(
analysis::getWindow, LinkedHashMap::new, Collectors.toList()));

for (Map.Entry<Analysis.ResolvedWindow, List<FunctionCall>> entry : functions.entrySet()) {
Analysis.ResolvedWindow window = entry.getKey();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,24 +97,22 @@ public void testSwapWindowFunctions() {
"SELECT sum(s1) OVER (PARTITION BY tag1, s1), min(s1) OVER (PARTITION BY tag1) FROM table1";
LogicalQueryPlan logicalQueryPlan2 = planTester.createPlan(sql2);

// Two window function has swapped, but the query plan remains the same
// Two window functions have swapped. Since the initial sort by (tag1, s1) satisfies both
// windows, no extra sort is needed between them.
/*
* └──OutputNode
* └──ProjectNode
* └──WindowNode(PARTITION BY tag1, s1)
* └──SortNode
* └──WindowNode(PARTITION BY tag1)
* └──SortNode
* └──TableScanNode
* └──WindowNode(PARTITION BY tag1)
* └──SortNode
* └──TableScanNode
*/
assertPlan(
logicalQueryPlan2,
output(
project(
window(
ImmutableList.of("tag1", "s1"),
ImmutableList.of(),
sort(window(sort(tableScan)))))));
ImmutableList.of("tag1", "s1"), ImmutableList.of(), window(sort(tableScan))))));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -212,7 +212,7 @@ public static WindowNode deserialize(ByteBuffer buffer) {
DataOrganizationSpecification specification = DataOrganizationSpecification.deserialize(buffer);
int preSortedOrderPrefix = ReadWriteIOUtils.readInt(buffer);
size = ReadWriteIOUtils.readInt(buffer);
Map<Symbol, Function> windowFunctions = new HashMap<>(size);
Map<Symbol, Function> windowFunctions = new LinkedHashMap<>(size);
for (int i = 0; i < size; i++) {
Symbol symbol = Symbol.deserialize(buffer);
Function function = new Function(buffer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ public <R, C> R accept(IAstVisitor<R, C> visitor, C context) {
public List<Node> getChildren() {
ImmutableList.Builder<Node> nodes = ImmutableList.builder();
nodes.addAll(arguments);
window.ifPresent(window -> nodes.add((Node) window));
return nodes.build();
}

Expand All @@ -190,14 +191,16 @@ public boolean equals(Object obj) {
}
FunctionCall o = (FunctionCall) obj;
return Objects.equals(name, o.name)
&& Objects.equals(window, o.window)
&& Objects.equals(nullTreatment, o.nullTreatment)
&& Objects.equals(distinct, o.distinct)
&& Objects.equals(processingMode, o.processingMode)
&& Objects.equals(arguments, o.arguments);
}

@Override
public int hashCode() {
return Objects.hash(name, distinct, processingMode, arguments);
return Objects.hash(name, window, nullTreatment, distinct, processingMode, arguments);
}

public enum NullTreatment {
Expand All @@ -214,6 +217,8 @@ public boolean shallowEquals(Node other) {
FunctionCall otherFunction = (FunctionCall) other;

return name.equals(otherFunction.name)
&& window.isPresent() == otherFunction.window.isPresent()
&& nullTreatment.equals(otherFunction.nullTreatment)
&& distinct == otherFunction.distinct
&& processingMode.equals(otherFunction.processingMode);
}
Expand Down
Loading