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 @@ -2156,6 +2156,42 @@ public void testWindowAggregationV2()
assertEquals(rows.get(i - 2).get(1).asLong(), row.get(2).asLong());
assertEquals(row.get(1).asLong() - row.get(2).asLong(), row.get(3).asLong());
}

query = "WITH tmp AS (\n"
+ " select count(*) as num_trips, DaysSinceEpoch from mytable GROUP BY DaysSinceEpoch\n"
+ ")\n"
+ "\n"
+ "SELECT\n"
+ " DaysSinceEpoch,\n"
+ " num_trips,\n"
+ " LAG(num_trips, '2') OVER (ORDER BY DaysSinceEpoch) AS previous_num_trips,\n"
+ " num_trips - LAG(num_trips, '2') OVER (ORDER BY DaysSinceEpoch) AS difference\n"
+ "FROM\n"
+ " tmp";
response = postQuery(query);
resultTable = response.get("resultTable");
assertEquals(resultTable.get("dataSchema").get("columnDataTypes").toString(),
"[\"INT\",\"LONG\",\"LONG\",\"LONG\"]");
rows = resultTable.get("rows");
assertEquals(rows.size(), 364);
for (int i = 0; i < 2; i++) {
JsonNode row = rows.get(i);
JsonNode tmpTableRow = tmpTableResult.get(i);
assertEquals(row.size(), 4);
assertEquals(row.get(0).asInt(), tmpTableRow.get(0).asInt());
assertEquals(row.get(1).asLong(), tmpTableRow.get(1).asLong());
assertTrue(row.get(2).isNull());
assertTrue(row.get(2).isNull());
}
for (int i = 2; i < 363; i++) {
JsonNode row = rows.get(i);
assertEquals(row.size(), 4);
JsonNode tmpTableRow = tmpTableResult.get(i);
assertEquals(row.get(0).asInt(), tmpTableRow.get(0).asInt());
assertEquals(row.get(1).asLong(), tmpTableRow.get(1).asLong());
assertEquals(rows.get(i - 2).get(1).asLong(), row.get(2).asLong());
assertEquals(row.get(1).asLong() - row.get(2).asLong(), row.get(3).asLong());
}
}

@Test(dataProvider = "useBothQueryEngines")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.apache.calcite.rel.type.RelDataTypeField;
import org.apache.calcite.rex.RexBuilder;
import org.apache.calcite.rex.RexInputRef;
import org.apache.calcite.rex.RexLiteral;
import org.apache.calcite.rex.RexNode;
import org.apache.calcite.sql.SqlAggFunction;
import org.apache.calcite.sql.SqlKind;
Expand Down Expand Up @@ -172,6 +173,17 @@ private Window.Group updateLiteralArgumentsInWindowGroup(Window window) {
newRexNode = window.constants.get(inputRefIndex - windowInputSize);
changed = true;
aggCallChanged = true;
} else {
RelNode windowInputRelNode = ((HepRelVertex) window.getInput()).getCurrentRel();
if (windowInputRelNode instanceof LogicalProject) {
RexNode inputRefRexNode = ((LogicalProject) windowInputRelNode).getProjects().get(inputRefIndex);
if (inputRefRexNode instanceof RexLiteral) {
// If the input reference is a literal, replace it with the literal value
newRexNode = inputRefRexNode;
changed = true;
aggCallChanged = true;
}
}
}
}
rexList.add(newRexNode);
Expand Down