提交 2e060308 authored 作者: Thomas Mueller's avatar Thomas Mueller

Invalid tables names in 'order by' columns were not detected in some cases.…

Invalid tables names in 'order by' columns were not detected in some cases. Example: select x from dual order by y.x
上级 6ff25122
......@@ -321,7 +321,8 @@ public abstract class Query extends Prepared {
ArrayList<String> expressionSQL,
ArrayList<SelectOrderBy> orderList,
int visible,
boolean mustBeInResult) {
boolean mustBeInResult,
ArrayList<TableFilter> filters) {
for (SelectOrderBy o : orderList) {
Expression e = o.expression;
if (e == null) {
......@@ -334,6 +335,7 @@ public abstract class Query extends Prepared {
boolean isAlias = false;
int idx = expressions.size();
if (e instanceof ExpressionColumn) {
// order by expression
ExpressionColumn exprCol = (ExpressionColumn) e;
String tableAlias = exprCol.getOriginalTableAliasName();
String col = exprCol.getOriginalColumnName();
......@@ -341,12 +343,23 @@ public abstract class Query extends Prepared {
boolean found = false;
Expression ec = expressions.get(j);
if (ec instanceof ExpressionColumn) {
// select expression
ExpressionColumn c = (ExpressionColumn) ec;
found = col.equals(c.getColumnName());
if (tableAlias != null && found) {
if (found && tableAlias != null) {
String ca = c.getOriginalTableAliasName();
if (ca != null) {
found = tableAlias.equals(ca);
if (ca == null) {
found = false;
// select id from test order by test.id
for (int i = 0, size = filters.size(); i < size; i++) {
TableFilter f = filters.get(i);
if (f.getTableAlias().equals(tableAlias)) {
found = true;
break;
}
}
} else {
found = ca.equals(tableAlias);
}
}
} else if (!(ec instanceof Alias)) {
......
......@@ -706,7 +706,7 @@ public class Select extends Query {
expressionSQL = null;
}
if (orderList != null) {
initOrder(expressions, expressionSQL, orderList, visibleColumnCount, distinct);
initOrder(expressions, expressionSQL, orderList, visibleColumnCount, distinct, filters);
}
distinctColumnCount = expressions.size();
if (having != null) {
......
......@@ -286,7 +286,7 @@ public class SelectUnion extends Query {
expressions.add(e);
}
if (orderList != null) {
initOrder(expressions, null, orderList, getColumnCount(), true);
initOrder(expressions, null, orderList, getColumnCount(), true, null);
sort = prepareOrder(orderList, expressions.size());
orderList = null;
}
......
--- special grammar and test cases ---------------------------------------------------------------------------------------------
select x from dual order by y.x;
> exception
create table test(id int primary key, name varchar(255), row_number int);
> ok
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论