提交 cbb6932a authored 作者: Thomas Mueller's avatar Thomas Mueller

If a query that was used like a table contained group by and was ordered by an…

If a query that was used like a table contained group by and was ordered by an expression that is not in the column list, an exception was thrown.
上级 5e05b9fc
......@@ -324,7 +324,6 @@ public abstract class Query extends Prepared {
String sql = e.getSQL();
expressionSQL.add(sql);
}
o.expression = null;
o.columnIndexExpr = ValueExpression.get(ValueInt.get(idx + 1));
}
}
......@@ -344,9 +343,6 @@ public abstract class Query extends Prepared {
SelectOrderBy o = (SelectOrderBy) orderList.get(i);
int idx;
boolean reverse = false;
if (o.expression != null) {
throw Message.getInternalError();
}
Expression expr = o.columnIndexExpr;
Value v = expr.getValue(null);
if (v == ValueNull.INSTANCE) {
......
......@@ -42,11 +42,11 @@ public class SelectOrderBy {
public String getSQL() {
StringBuffer buff = new StringBuffer();
if (columnIndexExpr != null) {
buff.append(columnIndexExpr.getSQL());
} else {
if (expression != null) {
buff.append("=");
buff.append(expression.getSQL());
} else {
buff.append(columnIndexExpr.getSQL());
}
if (descending) {
buff.append(" DESC");
......
--- special grammar and test cases ---------------------------------------------------------------------------------------------
select * from(select 1 from system_range(1, 2) group by sin(x) order by sin(x));
> 1
> -
> 1
> 1
> rows (ordered): 2
create table parent as select 1 id, 2 x;
> ok
......
--- special grammar and test cases ---------------------------------------------------------------------------------------------
select * from(select 1 from system_range(1, 2) group by sin(x) order by sin(x));
> 1
> -
> 1
> 1
> rows (ordered): 2
create table parent as select 1 id, 2 x;
> ok
......@@ -422,7 +429,7 @@ insert into t2 select x from system_range(1, 1000);
explain select count(*) from t1 where t1.id in ( select t2.id from t2 );
> PLAN
> -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
> SELECT COUNT(*) FROM (SELECT DISTINCT T2.ID AS TEMP_VIEW_3_X FROM PUBLIC.T2 /* PUBLIC.T2_TABLE_SCAN */) TEMP_VIEW_4 /* SELECT DISTINCT T2.ID AS TEMP_VIEW_3_X FROM PUBLIC.T2 /++ PUBLIC.T2_TABLE_SCAN ++/ */ INNER JOIN PUBLIC.T1 /* PUBLIC.PRIMARY_KEY_A: ID = TEMP_VIEW_4.TEMP_VIEW_3_X */ ON 1=1 WHERE T1.ID = TEMP_VIEW_4.TEMP_VIEW_3_X
> SELECT COUNT(*) FROM (SELECT DISTINCT T2.ID AS TEMP_VIEW_4_X FROM PUBLIC.T2 /* PUBLIC.T2_TABLE_SCAN */) TEMP_VIEW_5 /* SELECT DISTINCT T2.ID AS TEMP_VIEW_4_X FROM PUBLIC.T2 /++ PUBLIC.T2_TABLE_SCAN ++/ */ INNER JOIN PUBLIC.T1 /* PUBLIC.PRIMARY_KEY_A: ID = TEMP_VIEW_5.TEMP_VIEW_4_X */ ON 1=1 WHERE T1.ID = TEMP_VIEW_5.TEMP_VIEW_4_X
> rows: 1
select count(*) from t1 where t1.id in ( select t2.id from t2 );
......@@ -869,7 +876,7 @@ insert into test values(1), (2), (3);
explain select * from test where id in(1, 2, null);
> PLAN
> ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
> SELECT TEST.ID FROM TABLE_DISTINCT(TEMP_VIEW_21_X INTEGER=(1, 2, NULL)) TEMP_VIEW_22 /* PUBLIC."" */ INNER JOIN PUBLIC.TEST /* PUBLIC.PRIMARY_KEY_2: ID >= 1 AND ID <= 2 AND ID = TEMP_VIEW_22.TEMP_VIEW_21_X */ ON 1=1 WHERE (ID IN(1, 2, NULL)) AND (ID = TEMP_VIEW_22.TEMP_VIEW_21_X)
> SELECT TEST.ID FROM TABLE_DISTINCT(TEMP_VIEW_22_X INTEGER=(1, 2, NULL)) TEMP_VIEW_23 /* PUBLIC."" */ INNER JOIN PUBLIC.TEST /* PUBLIC.PRIMARY_KEY_2: ID >= 1 AND ID <= 2 AND ID = TEMP_VIEW_23.TEMP_VIEW_22_X */ ON 1=1 WHERE (ID IN(1, 2, NULL)) AND (ID = TEMP_VIEW_23.TEMP_VIEW_22_X)
> rows: 1
drop table test;
......@@ -2590,7 +2597,7 @@ select * from test t1 where id in(id);
explain select * from test t1 where id in(select id from test);
> PLAN
> ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
> SELECT T1.ID, T1.NAME FROM (SELECT DISTINCT ID AS TEMP_VIEW_71_X FROM PUBLIC.TEST /* PUBLIC.TEST_TABLE_SCAN */) TEMP_VIEW_72 /* SELECT DISTINCT ID AS TEMP_VIEW_71_X FROM PUBLIC.TEST /++ PUBLIC.TEST_TABLE_SCAN ++/ */ INNER JOIN PUBLIC.TEST T1 /* PUBLIC.PRIMARY_KEY_2: ID = TEMP_VIEW_72.TEMP_VIEW_71_X */ ON 1=1 WHERE ID = TEMP_VIEW_72.TEMP_VIEW_71_X
> SELECT T1.ID, T1.NAME FROM (SELECT DISTINCT ID AS TEMP_VIEW_72_X FROM PUBLIC.TEST /* PUBLIC.TEST_TABLE_SCAN */) TEMP_VIEW_73 /* SELECT DISTINCT ID AS TEMP_VIEW_72_X FROM PUBLIC.TEST /++ PUBLIC.TEST_TABLE_SCAN ++/ */ INNER JOIN PUBLIC.TEST T1 /* PUBLIC.PRIMARY_KEY_2: ID = TEMP_VIEW_73.TEMP_VIEW_72_X */ ON 1=1 WHERE ID = TEMP_VIEW_73.TEMP_VIEW_72_X
> rows: 1
select * from test t1 where id in(select id from test);
......@@ -2603,7 +2610,7 @@ select * from test t1 where id in(select id from test);
explain select * from test t1 where id in(1, select max(id) from test);
> PLAN
> --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
> SELECT T1.ID, T1.NAME FROM TABLE_DISTINCT(TEMP_VIEW_79_X INTEGER=(1, 2)) TEMP_VIEW_80 /* PUBLIC."" */ INNER JOIN PUBLIC.TEST T1 /* PUBLIC.PRIMARY_KEY_2: ID >= 1 AND ID <= 2 AND ID = TEMP_VIEW_80.TEMP_VIEW_79_X */ ON 1=1 WHERE (ID IN(1, 2)) AND (ID = TEMP_VIEW_80.TEMP_VIEW_79_X)
> SELECT T1.ID, T1.NAME FROM TABLE_DISTINCT(TEMP_VIEW_80_X INTEGER=(1, 2)) TEMP_VIEW_81 /* PUBLIC."" */ INNER JOIN PUBLIC.TEST T1 /* PUBLIC.PRIMARY_KEY_2: ID >= 1 AND ID <= 2 AND ID = TEMP_VIEW_81.TEMP_VIEW_80_X */ ON 1=1 WHERE (ID IN(1, 2)) AND (ID = TEMP_VIEW_81.TEMP_VIEW_80_X)
> rows: 1
select * from test t1 where id in(1, select max(id) from test);
......@@ -5506,13 +5513,13 @@ EXPLAIN PLAN FOR SELECT * FROM TEST T1 WHERE EXISTS(SELECT * FROM TEST T2 WHERE
EXPLAIN PLAN FOR SELECT * FROM TEST T1 WHERE ID IN(1, 2);
> PLAN
> --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
> SELECT T1.ID, T1.NAME FROM TABLE_DISTINCT(TEMP_VIEW_116_X INTEGER=(1, 2)) TEMP_VIEW_117 /* PUBLIC."" */ INNER JOIN PUBLIC.TEST T1 /* PUBLIC.PRIMARY_KEY_2: ID >= 1 AND ID <= 2 AND ID = TEMP_VIEW_117.TEMP_VIEW_116_X */ ON 1=1 WHERE (ID IN(1, 2)) AND (ID = TEMP_VIEW_117.TEMP_VIEW_116_X)
> SELECT T1.ID, T1.NAME FROM TABLE_DISTINCT(TEMP_VIEW_117_X INTEGER=(1, 2)) TEMP_VIEW_118 /* PUBLIC."" */ INNER JOIN PUBLIC.TEST T1 /* PUBLIC.PRIMARY_KEY_2: ID >= 1 AND ID <= 2 AND ID = TEMP_VIEW_118.TEMP_VIEW_117_X */ ON 1=1 WHERE (ID IN(1, 2)) AND (ID = TEMP_VIEW_118.TEMP_VIEW_117_X)
> rows: 1
EXPLAIN PLAN FOR SELECT * FROM TEST T1 WHERE ID IN(SELECT ID FROM TEST);
> PLAN
> -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
> SELECT T1.ID, T1.NAME FROM (SELECT DISTINCT ID AS TEMP_VIEW_120_X FROM PUBLIC.TEST /* PUBLIC.TEST_TABLE_SCAN */) TEMP_VIEW_121 /* SELECT DISTINCT ID AS TEMP_VIEW_120_X FROM PUBLIC.TEST /++ PUBLIC.TEST_TABLE_SCAN ++/ */ INNER JOIN PUBLIC.TEST T1 /* PUBLIC.PRIMARY_KEY_2: ID = TEMP_VIEW_121.TEMP_VIEW_120_X */ ON 1=1 WHERE ID = TEMP_VIEW_121.TEMP_VIEW_120_X
> SELECT T1.ID, T1.NAME FROM (SELECT DISTINCT ID AS TEMP_VIEW_121_X FROM PUBLIC.TEST /* PUBLIC.TEST_TABLE_SCAN */) TEMP_VIEW_122 /* SELECT DISTINCT ID AS TEMP_VIEW_121_X FROM PUBLIC.TEST /++ PUBLIC.TEST_TABLE_SCAN ++/ */ INNER JOIN PUBLIC.TEST T1 /* PUBLIC.PRIMARY_KEY_2: ID = TEMP_VIEW_122.TEMP_VIEW_121_X */ ON 1=1 WHERE ID = TEMP_VIEW_122.TEMP_VIEW_121_X
> rows: 1
EXPLAIN PLAN FOR SELECT * FROM TEST T1 WHERE ID NOT IN(SELECT ID FROM TEST);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论