提交 6e8f8506 authored 作者: Thomas Mueller's avatar Thomas Mueller

Array literals with zero or one element were not be parsed correctly.

上级 d2353456
...@@ -1506,11 +1506,14 @@ ID=1 AND NAME='Hi' ...@@ -1506,11 +1506,14 @@ ID=1 AND NAME='Hi'
" "
"Other Grammar","Array"," "Other Grammar","Array","
( expression [,...] ) ( [ expression, [ expression [,...] ] ] )
"," ","
An array of values. An array of values. An empty array is '()'. Trailing commas are ignored.
An array with one element must contain a comma to be parsed as an array.
"," ","
(1, 2) (1, 2)
(1, )
()
" "
"Other Grammar","Boolean"," "Other Grammar","Boolean","
......
...@@ -18,7 +18,10 @@ Change Log ...@@ -18,7 +18,10 @@ Change Log
<h1>Change Log</h1> <h1>Change Log</h1>
<h2>Next Version (unreleased)</h2> <h2>Next Version (unreleased)</h2>
<ul><li>Issue 290: Conditions using subqueries were sometimes evaluated before much simpler conditions, <ul><li>Array literals with zero or one element were not be parsed correctly.
Now trailing commas are ignored (as in Java). Expressions of the form '(1)' are still parsed as the value 1.
An empty array is '()'. An array with one element must contain a comma to be parsed as an array, as in '(1,)'.
</li><li>Issue 290: Conditions using subqueries were sometimes evaluated before much simpler conditions,
which was very bad for performance. This includes subqueries returning a value, EXISTS subqueries, which was very bad for performance. This includes subqueries returning a value, EXISTS subqueries,
and IN(SELECT ..) subqueries. and IN(SELECT ..) subqueries.
</li><li>Issue 288: Some queries with right outer joins or nested joins could throw a NullPointerException. </li><li>Issue 288: Some queries with right outer joins or nested joins could throw a NullPointerException.
......
...@@ -561,6 +561,7 @@ See also <a href="build.html#providing_patches">Providing Patches</a>. ...@@ -561,6 +561,7 @@ See also <a href="build.html#providing_patches">Providing Patches</a>.
</li><li>Optimization for large lists for column IN(1, 2, 3, 4,...) - currently an list is used, could potentially use a hash set </li><li>Optimization for large lists for column IN(1, 2, 3, 4,...) - currently an list is used, could potentially use a hash set
(maybe only for a part of the values - the ones that can be evaluated). (maybe only for a part of the values - the ones that can be evaluated).
</li><li>Compatibility for ARRAY data type (Oracle: VARRAY(n) of VARCHAR(m); HSQLDB: VARCHAR(n) ARRAY; Postgres: VARCHAR(n)[]). </li><li>Compatibility for ARRAY data type (Oracle: VARRAY(n) of VARCHAR(m); HSQLDB: VARCHAR(n) ARRAY; Postgres: VARCHAR(n)[]).
</li><li>PostgreSQL compatible array literal syntax: ARRAY[['breakfast', 'consulting'], ['meeting', 'lunch']]
</li></ul> </li></ul>
<h2>Not Planned</h2> <h2>Not Planned</h2>
......
...@@ -2482,19 +2482,28 @@ public class Parser { ...@@ -2482,19 +2482,28 @@ public class Parser {
break; break;
case OPEN: case OPEN:
read(); read();
r = readExpression(); if (readIf(")")) {
if (readIf(",")) { r = ExpressionList.EMPTY;
ArrayList<Expression> list = New.arrayList(); } else {
list.add(r); r = readExpression();
do { if (readIf(",")) {
r = readExpression(); ArrayList<Expression> list = New.arrayList();
list.add(r); list.add(r);
} while (readIf(",")); while (!readIf(")")) {
Expression[] array = new Expression[list.size()]; r = readExpression();
list.toArray(array); list.add(r);
r = new ExpressionList(array); if (!readIf(",")) {
read(")");
break;
}
}
Expression[] array = new Expression[list.size()];
list.toArray(array);
r = new ExpressionList(array);
} else {
read(")");
}
} }
read(")");
break; break;
case TRUE: case TRUE:
read(); read();
......
...@@ -20,6 +20,8 @@ import org.h2.value.ValueArray; ...@@ -20,6 +20,8 @@ import org.h2.value.ValueArray;
*/ */
public class ExpressionList extends Expression { public class ExpressionList extends Expression {
public static final ExpressionList EMPTY = new ExpressionList(new Expression[0]);
private Expression[] list; private Expression[] list;
public ExpressionList(Expression[] list) { public ExpressionList(Expression[] list) {
...@@ -83,6 +85,9 @@ public class ExpressionList extends Expression { ...@@ -83,6 +85,9 @@ public class ExpressionList extends Expression {
buff.appendExceptFirst(", "); buff.appendExceptFirst(", ");
buff.append(e.getSQL()); buff.append(e.getSQL());
} }
if (list.length == 1) {
buff.append(',');
}
return buff.append(')').toString(); return buff.append(')').toString();
} }
......
...@@ -15,6 +15,9 @@ import org.h2.util.StatementBuilder; ...@@ -15,6 +15,9 @@ import org.h2.util.StatementBuilder;
* Implementation of the ARRAY data type. * Implementation of the ARRAY data type.
*/ */
public class ValueArray extends Value { public class ValueArray extends Value {
private static final ValueArray EMPTY = new ValueArray(new Value[0]);
private final Value[] values; private final Value[] values;
private int hash; private int hash;
...@@ -30,7 +33,7 @@ public class ValueArray extends Value { ...@@ -30,7 +33,7 @@ public class ValueArray extends Value {
* @return the value * @return the value
*/ */
public static ValueArray get(Value[] list) { public static ValueArray get(Value[] list) {
return new ValueArray(list); return list.length == 0 ? EMPTY : new ValueArray(list);
} }
public int hashCode() { public int hashCode() {
...@@ -104,6 +107,9 @@ public class ValueArray extends Value { ...@@ -104,6 +107,9 @@ public class ValueArray extends Value {
buff.appendExceptFirst(", "); buff.appendExceptFirst(", ");
buff.append(v.getSQL()); buff.append(v.getSQL());
} }
if (values.length == 1) {
buff.append(',');
}
return buff.append(')').toString(); return buff.append(')').toString();
} }
......
--- special grammar and test cases --------------------------------------------------------------------------------------------- --- special grammar and test cases ---------------------------------------------------------------------------------------------
select () empty;
> EMPTY
> -----
> ()
> rows: 1
select (1,) one_element;
> ONE_ELEMENT
> -----------
> (1)
> rows: 1
select (1) one;
> ONE
> ---
> 1
> rows: 1
create table test(id int); create table test(id int);
> ok > ok
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论