提交 99c866a3 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Use another approach to distinguish between quantified comparison and aggregate

上级 248b5417
...@@ -1405,18 +1405,21 @@ public class Parser { ...@@ -1405,18 +1405,21 @@ public class Parser {
return prep; return prep;
} }
private boolean isSelect() { private boolean isSelectDeep() {
int start = lastParseIndex; int start = lastParseIndex;
while (readIf(OPEN_PAREN)) { while (readIf(OPEN_PAREN)) {
// need to read ahead, it could be a nested union: // need to read ahead, it could be a nested union:
// ((select 1) union (select 1)) // ((select 1) union (select 1))
} }
boolean select = isToken(SELECT) || isToken(FROM) || isToken(WITH); boolean select = isSelect();
parseIndex = start; parseIndex = start;
read(); read();
return select; return select;
} }
private boolean isSelect() {
return isToken(SELECT) || isToken(FROM) || isToken(WITH);
}
private Prepared parseMerge() { private Prepared parseMerge() {
int start = lastParseIndex; int start = lastParseIndex;
...@@ -1431,7 +1434,7 @@ public class Parser { ...@@ -1431,7 +1434,7 @@ public class Parser {
command.setTargetTableFilter(targetTableFilter); command.setTargetTableFilter(targetTableFilter);
Table table = command.getTargetTable(); Table table = command.getTargetTable();
if (readIf(OPEN_PAREN)) { if (readIf(OPEN_PAREN)) {
if (isSelect()) { if (isSelectDeep()) {
command.setQuery(parseSelect()); command.setQuery(parseSelect());
read(CLOSE_PAREN); read(CLOSE_PAREN);
return command; return command;
...@@ -1461,7 +1464,7 @@ public class Parser { ...@@ -1461,7 +1464,7 @@ public class Parser {
if (readIf(OPEN_PAREN)) { if (readIf(OPEN_PAREN)) {
/* a select query is supplied */ /* a select query is supplied */
if (isSelect()) { if (isSelectDeep()) {
command.setQuery(parseSelect()); command.setQuery(parseSelect());
read(CLOSE_PAREN); read(CLOSE_PAREN);
} }
...@@ -1616,7 +1619,7 @@ public class Parser { ...@@ -1616,7 +1619,7 @@ public class Parser {
private Insert parseInsertGivenTable(Insert command, Table table) { private Insert parseInsertGivenTable(Insert command, Table table) {
Column[] columns = null; Column[] columns = null;
if (readIf(OPEN_PAREN)) { if (readIf(OPEN_PAREN)) {
if (isSelect()) { if (isSelectDeep()) {
command.setQuery(parseSelect()); command.setQuery(parseSelect());
read(CLOSE_PAREN); read(CLOSE_PAREN);
return command; return command;
...@@ -1669,7 +1672,7 @@ public class Parser { ...@@ -1669,7 +1672,7 @@ public class Parser {
Table table = readTableOrView(); Table table = readTableOrView();
command.setTable(table); command.setTable(table);
if (readIf(OPEN_PAREN)) { if (readIf(OPEN_PAREN)) {
if (isSelect()) { if (isSelectDeep()) {
command.setQuery(parseSelect()); command.setQuery(parseSelect());
read(CLOSE_PAREN); read(CLOSE_PAREN);
return command; return command;
...@@ -1706,7 +1709,7 @@ public class Parser { ...@@ -1706,7 +1709,7 @@ public class Parser {
Table table; Table table;
String alias = null; String alias = null;
label: if (readIf(OPEN_PAREN)) { label: if (readIf(OPEN_PAREN)) {
if (isSelect()) { if (isSelectDeep()) {
Query query = parseSelectUnion(); Query query = parseSelectUnion();
read(CLOSE_PAREN); read(CLOSE_PAREN);
query.setParameterList(new ArrayList<>(parameters)); query.setParameterList(new ArrayList<>(parameters));
...@@ -2822,7 +2825,7 @@ public class Parser { ...@@ -2822,7 +2825,7 @@ public class Parser {
} }
r = ValueExpression.get(ValueBoolean.FALSE); r = ValueExpression.get(ValueBoolean.FALSE);
} else { } else {
if (isSelect()) { if (isSelectDeep()) {
Query query = parseSelect(); Query query = parseSelect();
r = new ConditionInSelect(database, r, query, false, r = new ConditionInSelect(database, r, query, false,
Comparison.EQUAL); Comparison.EQUAL);
...@@ -2862,23 +2865,33 @@ public class Parser { ...@@ -2862,23 +2865,33 @@ public class Parser {
break; break;
} }
read(); read();
int start = lastParseIndex;
if (readIf(ALL)) { if (readIf(ALL)) {
read(OPEN_PAREN); read(OPEN_PAREN);
if (isSelect()) {
Query query = parseSelect(); Query query = parseSelect();
r = new ConditionInSelect(database, r, query, true, r = new ConditionInSelect(database, r, query, true, compareType);
compareType);
read(CLOSE_PAREN); read(CLOSE_PAREN);
} else {
parseIndex = start;
read();
r = new Comparison(session, compareType, r, readConcat());
}
} else if (readIf("ANY") || readIf("SOME")) { } else if (readIf("ANY") || readIf("SOME")) {
read(OPEN_PAREN); read(OPEN_PAREN);
if (currentTokenType == PARAMETER && compareType == 0) { if (currentTokenType == PARAMETER && compareType == 0) {
Parameter p = readParameter(); Parameter p = readParameter();
r = new ConditionInParameter(database, r, p); r = new ConditionInParameter(database, r, p);
} else { read(CLOSE_PAREN);
} else if (isSelect()) {
Query query = parseSelect(); Query query = parseSelect();
r = new ConditionInSelect(database, r, query, false, r = new ConditionInSelect(database, r, query, false, compareType);
compareType);
}
read(CLOSE_PAREN); read(CLOSE_PAREN);
} else {
parseIndex = start;
read();
r = new Comparison(session, compareType, r, readConcat());
}
} else { } else {
r = new Comparison(session, compareType, r, readConcat()); r = new Comparison(session, compareType, r, readConcat());
} }
......
...@@ -19,6 +19,7 @@ import org.h2.engine.Session; ...@@ -19,6 +19,7 @@ import org.h2.engine.Session;
import org.h2.expression.Expression; import org.h2.expression.Expression;
import org.h2.expression.ExpressionColumn; import org.h2.expression.ExpressionColumn;
import org.h2.expression.ExpressionVisitor; import org.h2.expression.ExpressionVisitor;
import org.h2.expression.Subquery;
import org.h2.expression.analysis.Window; import org.h2.expression.analysis.Window;
import org.h2.index.Cursor; import org.h2.index.Cursor;
import org.h2.index.Index; import org.h2.index.Index;
...@@ -812,7 +813,12 @@ public class Aggregate extends AbstractAggregate { ...@@ -812,7 +813,12 @@ public class Aggregate extends AbstractAggregate {
on.getSQL(builder).append(')'); on.getSQL(builder).append(')');
} else { } else {
builder.append('('); builder.append('(');
on.getUnenclosedSQL(builder).append(')'); if (on instanceof Subquery) {
on.getSQL(builder);
} else {
on.getUnenclosedSQL(builder);
}
builder.append(')');
} }
return appendTailConditions(builder); return appendTailConditions(builder);
} }
......
...@@ -19,3 +19,15 @@ SELECT A, ANY(B < 2), SOME(B > 3), BOOL_OR(B = 1), ANY(B = 1) FILTER (WHERE A = ...@@ -19,3 +19,15 @@ SELECT A, ANY(B < 2), SOME(B > 3), BOOL_OR(B = 1), ANY(B = 1) FILTER (WHERE A =
DROP TABLE TEST; DROP TABLE TEST;
> ok > ok
SELECT TRUE = ANY((SELECT TRUE));
> TRUE = ANY((SELECT TRUE FROM SYSTEM_RANGE(1, 1) /* PUBLIC.RANGE_INDEX */ /* scanCount: 2 */))
> ---------------------------------------------------------------------------------------------
> TRUE
> rows: 1
SELECT TRUE = ANY((SELECT FALSE));
> TRUE = ANY((SELECT FALSE FROM SYSTEM_RANGE(1, 1) /* PUBLIC.RANGE_INDEX */ /* scanCount: 2 */))
> ----------------------------------------------------------------------------------------------
> FALSE
> rows: 1
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论