提交 4357cfdd authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Partially revert 99c866a3 and use parentheses instead

上级 41355dd0
...@@ -1405,22 +1405,18 @@ public class Parser { ...@@ -1405,22 +1405,18 @@ public class Parser {
return prep; return prep;
} }
private boolean isSelectDeep() { private boolean isSelect() {
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 = isSelect(); boolean select = isToken(SELECT) || isToken(FROM) || isToken(WITH);
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;
read("INTO"); read("INTO");
...@@ -1434,7 +1430,7 @@ public class Parser { ...@@ -1434,7 +1430,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 (isSelectDeep()) { if (isSelect()) {
command.setQuery(parseSelect()); command.setQuery(parseSelect());
read(CLOSE_PAREN); read(CLOSE_PAREN);
return command; return command;
...@@ -1464,7 +1460,7 @@ public class Parser { ...@@ -1464,7 +1460,7 @@ public class Parser {
if (readIf(OPEN_PAREN)) { if (readIf(OPEN_PAREN)) {
/* a select query is supplied */ /* a select query is supplied */
if (isSelectDeep()) { if (isSelect()) {
command.setQuery(parseSelect()); command.setQuery(parseSelect());
read(CLOSE_PAREN); read(CLOSE_PAREN);
} }
...@@ -1619,7 +1615,7 @@ public class Parser { ...@@ -1619,7 +1615,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 (isSelectDeep()) { if (isSelect()) {
command.setQuery(parseSelect()); command.setQuery(parseSelect());
read(CLOSE_PAREN); read(CLOSE_PAREN);
return command; return command;
...@@ -1672,7 +1668,7 @@ public class Parser { ...@@ -1672,7 +1668,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 (isSelectDeep()) { if (isSelect()) {
command.setQuery(parseSelect()); command.setQuery(parseSelect());
read(CLOSE_PAREN); read(CLOSE_PAREN);
return command; return command;
...@@ -1709,7 +1705,7 @@ public class Parser { ...@@ -1709,7 +1705,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 (isSelectDeep()) { if (isSelect()) {
Query query = parseSelectUnion(); Query query = parseSelectUnion();
read(CLOSE_PAREN); read(CLOSE_PAREN);
query.setParameterList(new ArrayList<>(parameters)); query.setParameterList(new ArrayList<>(parameters));
...@@ -2825,7 +2821,7 @@ public class Parser { ...@@ -2825,7 +2821,7 @@ public class Parser {
} }
r = ValueExpression.get(ValueBoolean.FALSE); r = ValueExpression.get(ValueBoolean.FALSE);
} else { } else {
if (isSelectDeep()) { if (isSelect()) {
Query query = parseSelect(); Query query = parseSelect();
r = new ConditionInSelect(database, r, query, false, r = new ConditionInSelect(database, r, query, false,
Comparison.EQUAL); Comparison.EQUAL);
......
...@@ -266,6 +266,15 @@ public class Aggregate extends AbstractAggregate { ...@@ -266,6 +266,15 @@ public class Aggregate extends AbstractAggregate {
this.groupConcatSeparator = separator; this.groupConcatSeparator = separator;
} }
/**
* Returns the type of this aggregate.
*
* @return the type of this aggregate
*/
public AggregateType getAggregateType() {
return type;
}
private void sortWithOrderBy(Value[] array) { private void sortWithOrderBy(Value[] array) {
final SortOrder sortOrder = orderBySort; final SortOrder sortOrder = orderBySort;
if (sortOrder != null) { if (sortOrder != null) {
......
...@@ -14,6 +14,8 @@ import org.h2.expression.ExpressionColumn; ...@@ -14,6 +14,8 @@ import org.h2.expression.ExpressionColumn;
import org.h2.expression.ExpressionVisitor; import org.h2.expression.ExpressionVisitor;
import org.h2.expression.Parameter; import org.h2.expression.Parameter;
import org.h2.expression.ValueExpression; import org.h2.expression.ValueExpression;
import org.h2.expression.aggregate.Aggregate;
import org.h2.expression.aggregate.Aggregate.AggregateType;
import org.h2.index.IndexCondition; import org.h2.index.IndexCondition;
import org.h2.message.DbException; import org.h2.message.DbException;
import org.h2.table.Column; import org.h2.table.Column;
...@@ -130,6 +132,7 @@ public class Comparison extends Condition { ...@@ -130,6 +132,7 @@ public class Comparison extends Condition {
@Override @Override
public StringBuilder getSQL(StringBuilder builder) { public StringBuilder getSQL(StringBuilder builder) {
boolean encloseRight = false;
builder.append('('); builder.append('(');
switch (compareType) { switch (compareType) {
case IS_NULL: case IS_NULL:
...@@ -143,9 +146,25 @@ public class Comparison extends Condition { ...@@ -143,9 +146,25 @@ public class Comparison extends Condition {
left.getSQL(builder).append(", "); left.getSQL(builder).append(", ");
right.getSQL(builder).append(')'); right.getSQL(builder).append(')');
break; break;
case EQUAL:
case BIGGER_EQUAL:
case BIGGER:
case SMALLER_EQUAL:
case SMALLER:
case NOT_EQUAL:
if (right instanceof Aggregate && ((Aggregate) right).getAggregateType() == AggregateType.ANY) {
encloseRight = true;
}
//$FALL-THROUGH$
default: default:
left.getSQL(builder).append(' ').append(getCompareOperator(compareType)).append(' '); left.getSQL(builder).append(' ').append(getCompareOperator(compareType)).append(' ');
if (encloseRight) {
builder.append('(');
}
right.getSQL(builder); right.getSQL(builder);
if (encloseRight) {
builder.append(')');
}
} }
return builder.append(')'); return builder.append(')');
} }
......
...@@ -20,14 +20,14 @@ SELECT A, ANY(B < 2), SOME(B > 3), BOOL_OR(B = 1), ANY(B = 1) FILTER (WHERE A = ...@@ -20,14 +20,14 @@ 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)); SELECT TRUE = (ANY((SELECT TRUE)));
> TRUE = ANY((SELECT TRUE FROM SYSTEM_RANGE(1, 1) /* PUBLIC.RANGE_INDEX */ /* scanCount: 2 */)) > TRUE = (ANY((SELECT TRUE FROM SYSTEM_RANGE(1, 1) /* PUBLIC.RANGE_INDEX */ /* scanCount: 2 */)))
> --------------------------------------------------------------------------------------------- > -----------------------------------------------------------------------------------------------
> TRUE > TRUE
> rows: 1 > rows: 1
SELECT TRUE = ANY((SELECT FALSE)); SELECT TRUE = (ANY((SELECT FALSE)));
> TRUE = ANY((SELECT FALSE FROM SYSTEM_RANGE(1, 1) /* PUBLIC.RANGE_INDEX */ /* scanCount: 2 */)) > TRUE = (ANY((SELECT FALSE FROM SYSTEM_RANGE(1, 1) /* PUBLIC.RANGE_INDEX */ /* scanCount: 2 */)))
> ---------------------------------------------------------------------------------------------- > ------------------------------------------------------------------------------------------------
> FALSE > FALSE
> rows: 1 > rows: 1
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论