未验证 提交 284d93c7 编写于 作者: V Vlad Ilyushchenko 提交者: GitHub

fix(griffin): when forming execution plan group-by key could get ignored in sub-queries (#752)

上级 883f1f05
......@@ -2177,6 +2177,18 @@ class SqlOptimiser {
}
}
// If this is group by model we need to add all non-selected keys, only if this is sub-query
// For top level models top-down column list will be empty
if (model.getSelectModelType() == QueryModel.SELECT_MODEL_GROUP_BY && model.getTopDownColumns().size() > 0) {
final ObjList<QueryColumn> bottomUpColumns = model.getBottomUpColumns();
for (int i = 0, n = bottomUpColumns.size(); i < n; i++) {
QueryColumn qc = bottomUpColumns.getQuick(i);
if (qc.getAst().type != FUNCTION || !functionParser.isGroupBy(qc.getAst().token)) {
model.addTopDownColumn(qc, qc.getAlias());
}
}
}
// latest by
emitLiteralsTopDown(model.getLatestBy(), model);
......
......@@ -49,6 +49,15 @@ public class SqlParserTest extends AbstractGriffinTest {
);
}
@Test
public void testGroupByTimeInSubQuery() throws SqlException {
assertQuery(
"select-group-by min(s) min, max(s) max, avg(s) avg from (select-group-by [sum(value) s, ts] ts, sum(value) s from (select [value, ts] from erdem_x timestamp (ts)))",
"select min(s), max(s), avg(s) from (select ts, sum(value) s from erdem_x)",
modelOf("erdem_x").timestamp("ts").col("value", ColumnType.LONG)
);
}
@Test
public void testAliasWithSpace() throws Exception {
assertQuery("select-choose x from (select [x] from x 'b a' where x > 1) 'b a'",
......@@ -4674,7 +4683,7 @@ public class SqlParserTest extends AbstractGriffinTest {
@Test
public void testSelectGroupByUnion() throws SqlException {
assertQuery("select-choose b from (select-group-by [sum(b) b] a, sum(b) b from (select [b] from trips) union all select-group-by [avg(d) b] c, avg(d) b from (select [d] from trips))",
assertQuery("select-choose b from (select-group-by [sum(b) b, a] a, sum(b) b from (select [b, a] from trips) union all select-group-by [avg(d) b, c] c, avg(d) b from (select [d, c] from trips))",
"select b from (select a, sum(b) b from trips union all select c, avg(d) b from trips)",
modelOf("trips")
.col("a", ColumnType.INT)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册