提交 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 ...@@ -19,6 +19,8 @@ Change Log
<h2>Next Version (unreleased)</h2> <h2>Next Version (unreleased)</h2>
<ul><li>Computed columns could not refer to itself. <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 </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). (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. </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 { ...@@ -114,40 +114,39 @@ public class Comparison extends Condition {
public String getSQL() { public String getSQL() {
String sql; String sql;
switch (compareType) { switch (compareType) {
case EQUAL: case IS_NULL:
sql = left.getSQL() + " = " + right.getSQL(); sql = left.getSQL() + " IS NULL";
break; break;
case EQUAL_NULL_SAFE: case IS_NOT_NULL:
sql = left.getSQL() + " IS " + right.getSQL(); sql = left.getSQL() + " IS NOT NULL";
break; 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: case BIGGER_EQUAL:
sql = left.getSQL() + " >= " + right.getSQL(); return ">=";
break;
case BIGGER: case BIGGER:
sql = left.getSQL() + " > " + right.getSQL(); return ">";
break;
case SMALLER_EQUAL: case SMALLER_EQUAL:
sql = left.getSQL() + " <= " + right.getSQL(); return "<=";
break;
case SMALLER: case SMALLER:
sql = left.getSQL() + " < " + right.getSQL(); return "<";
break;
case NOT_EQUAL: case NOT_EQUAL:
sql = left.getSQL() + " <> " + right.getSQL(); return "<>";
break;
case NOT_EQUAL_NULL_SAFE: case NOT_EQUAL_NULL_SAFE:
sql = left.getSQL() + " IS NOT " + right.getSQL(); return "IS NOT";
break;
case IS_NULL:
sql = left.getSQL() + " IS NULL";
break;
case IS_NOT_NULL:
sql = left.getSQL() + " IS NOT NULL";
break;
default: default:
throw DbException.throwInternalError("compareType=" + compareType); throw DbException.throwInternalError("compareType=" + compareType);
} }
return "(" + sql + ")";
} }
public Expression optimize(Session session) { public Expression optimize(Session session) {
......
...@@ -121,7 +121,17 @@ public class ConditionInSelect extends Condition { ...@@ -121,7 +121,17 @@ public class ConditionInSelect extends Condition {
} }
public String getSQL() { 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) { public void updateAggregate(Session session) {
......
--- special grammar and test cases --------------------------------------------------------------------------------------------- --- 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; create table test(id int) as select 1;
> ok > ok
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论