提交 937a20d9 authored 作者: Thomas Mueller's avatar Thomas Mueller

Issue 340: Comparison with "x = all(select ...)" or similar in a view or…

Issue 340: Comparison with "x = all(select ...)" or similar in a view or subquery that was used as a table returned the wrong result.
上级 21b84c37
......@@ -19,6 +19,8 @@ Change Log
<h2>Next Version (unreleased)</h2>
<ul><li>Computed columns could not refer to itself.
</li><li>Issue 340: Comparison with "x = all(select ...)" or similar in a view or
subquery that was used as a table returned the wrong result.
</li><li>Comparison with "x = all(select ...)" or similar returned the wrong result for some cases
(for example if the subquery returned no rows, or multiple rows with the same value, or null, or if x was null).
</li><li>Issue 335: Could not run DROP ALL OBJECTS DELETE FILES on older databases with CLOB or BLOB data.
......
......@@ -114,40 +114,39 @@ public class Comparison extends Condition {
public String getSQL() {
String sql;
switch (compareType) {
case EQUAL:
sql = left.getSQL() + " = " + right.getSQL();
case IS_NULL:
sql = left.getSQL() + " IS NULL";
break;
case EQUAL_NULL_SAFE:
sql = left.getSQL() + " IS " + right.getSQL();
case IS_NOT_NULL:
sql = left.getSQL() + " IS NOT NULL";
break;
default:
sql = left.getSQL() + " " + getCompareOperator(compareType) + " " + right.getSQL();
}
return "(" + sql + ")";
}
static String getCompareOperator(int compareType) {
switch (compareType) {
case EQUAL:
return "=";
case EQUAL_NULL_SAFE:
return "IS";
case BIGGER_EQUAL:
sql = left.getSQL() + " >= " + right.getSQL();
break;
return ">=";
case BIGGER:
sql = left.getSQL() + " > " + right.getSQL();
break;
return ">";
case SMALLER_EQUAL:
sql = left.getSQL() + " <= " + right.getSQL();
break;
return "<=";
case SMALLER:
sql = left.getSQL() + " < " + right.getSQL();
break;
return "<";
case NOT_EQUAL:
sql = left.getSQL() + " <> " + right.getSQL();
break;
return "<>";
case NOT_EQUAL_NULL_SAFE:
sql = left.getSQL() + " IS NOT " + right.getSQL();
break;
case IS_NULL:
sql = left.getSQL() + " IS NULL";
break;
case IS_NOT_NULL:
sql = left.getSQL() + " IS NOT NULL";
break;
return "IS NOT";
default:
throw DbException.throwInternalError("compareType=" + compareType);
}
return "(" + sql + ")";
}
public Expression optimize(Session session) {
......
......@@ -121,7 +121,17 @@ public class ConditionInSelect extends Condition {
}
public String getSQL() {
return "(" + left.getSQL() + " IN(\n" + StringUtils.indent(query.getPlanSQL(), 4, false) + "))";
StringBuilder buff = new StringBuilder();
buff.append('(').append(left.getSQL()).append(' ');
if (all) {
buff.append(Comparison.getCompareOperator(compareType)).
append(" ALL");
} else {
buff.append("IN");
}
buff.append("(\n").append(StringUtils.indent(query.getPlanSQL(), 4, false)).
append("))");
return buff.toString();
}
public void updateAggregate(Session session) {
......
--- special grammar and test cases ---------------------------------------------------------------------------------------------
create table test(name varchar(255));
> ok
insert into test values('a'), ('b'), ('c');
> update count: 3
select name from test where name > all(select name from test where name<'b');
> NAME
> ----
> b
> c
> rows: 2
select count(*) from (select name from test where name > all(select name from test where name<'b')) x;
> COUNT(*)
> --------
> 2
> rows: 1
drop table test;
> ok
create table test(id int) as select 1;
> ok
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论