Skip to content
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

[CALCITE-3662] Generate wrong SQL when plan contains Project(Sort(Aggregate)) and aggregate field has no alias #1715

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
[CALCITE-3662] Generate wrong SQL when plan contains Project(Sort(Agg…
…regate)) and aggregate field has no alias (Lei Jiang)
  • Loading branch information
helloppx authored and lei.jiang committed Jan 9, 2020
commit e6943c91e74fdd445e757528e9cf8c021e195224
Original file line number Diff line number Diff line change
Expand Up @@ -227,12 +227,8 @@ public Result visit(Project e) {
if (isStar(e.getChildExps(), e.getInput().getRowType(), e.getRowType())) {
return x;
}
final Builder builder;
if (e.getInput() instanceof Sort) {
builder = x.builder(e);
} else {
builder = x.builder(e, Clause.SELECT);
}
final Builder builder =
x.builder(e, Clause.SELECT);
final List<SqlNode> selectList = new ArrayList<>();
for (RexNode ref : e.getChildExps()) {
SqlNode sqlExpr = builder.context.toSql(null, ref);
Expand Down Expand Up @@ -638,6 +634,15 @@ public Result visit(Sort e) {
}
Result x = visitChild(0, e.getInput());
Builder builder = x.builder(e, Clause.ORDER_BY);
if (stack.size() != 1 && builder.select.getSelectList() == null) {
// Generates explicit column names instead of start(*) for
// non-root order by to avoid ambiguity.
final List<SqlNode> selectList = Expressions.list();
for (RelDataTypeField field : e.getRowType().getFieldList()) {
addSelect(selectList, builder.context.field(field.getIndex()), e.getRowType());
}
builder.select.setSelectList(new SqlNodeList(selectList, POS));
}
List<SqlNode> orderByList = Expressions.list();
for (RelFieldCollation field : e.getCollation().getFieldCollations()) {
builder.addOrderItem(orderByList, field);
Expand Down