提交 16459470 authored 作者: Uncle-pan's avatar Uncle-pan

Merge remote-tracking branch 'origin/master' into clean-test-master

...@@ -8,7 +8,7 @@ cache: ...@@ -8,7 +8,7 @@ cache:
matrix: matrix:
include: include:
- jdk: oraclejdk10 - jdk: openjdk10
dist: trusty dist: trusty
group: edge group: edge
sudo: required sudo: required
......
...@@ -3,14 +3,17 @@ ...@@ -3,14 +3,17 @@
# Initial Developer: H2 Group # Initial Developer: H2 Group
"SECTION","TOPIC","SYNTAX","TEXT","EXAMPLE" "SECTION","TOPIC","SYNTAX","TEXT","EXAMPLE"
"Commands (DML)","SELECT"," "Commands (DML)","SELECT","
SELECT [ TOP term ] [ DISTINCT | ALL ] selectExpression [,...] SELECT [ TOP term [ WITH TIES ] ]
[ DISTINCT [ ON ( expression [,...] ) ] | ALL ]
selectExpression [,...]
FROM tableExpression [,...] [ WHERE expression ] FROM tableExpression [,...] [ WHERE expression ]
[ GROUP BY expression [,...] ] [ HAVING expression ] [ GROUP BY expression [,...] ] [ HAVING expression ]
[ { UNION [ ALL ] | MINUS | EXCEPT | INTERSECT } select ] [ { UNION [ ALL ] | MINUS | EXCEPT | INTERSECT } select ]
[ ORDER BY order [,...] ] [ ORDER BY order [,...] ]
[ { LIMIT expression [ OFFSET expression ] [ SAMPLE_SIZE rowCountInt ] } [ { LIMIT expression [ OFFSET expression ] [ SAMPLE_SIZE rowCountInt ] }
| { [ OFFSET expression { ROW | ROWS } ] | { [ OFFSET expression { ROW | ROWS } ]
[ { FETCH { FIRST | NEXT } expression { ROW | ROWS } ONLY } ] } ] [ FETCH { FIRST | NEXT } expression { ROW | ROWS }
{ ONLY | WITH TIES } ] } ]
[ FOR UPDATE ] [ FOR UPDATE ]
"," ","
Selects data from a table or multiple tables. Selects data from a table or multiple tables.
...@@ -42,6 +45,8 @@ SELECT * FROM TEST LIMIT 1000; ...@@ -42,6 +45,8 @@ SELECT * FROM TEST LIMIT 1000;
SELECT * FROM (SELECT ID, COUNT(*) FROM TEST SELECT * FROM (SELECT ID, COUNT(*) FROM TEST
GROUP BY ID UNION SELECT NULL, COUNT(*) FROM TEST) GROUP BY ID UNION SELECT NULL, COUNT(*) FROM TEST)
ORDER BY 1 NULLS LAST; ORDER BY 1 NULLS LAST;
SELECT DISTINCT C1, C2 FROM TEST;
SELECT DISTINCT ON(C1) C1, C2 FROM TEST ORDER BY C1;
" "
"Commands (DML)","INSERT"," "Commands (DML)","INSERT","
......
...@@ -21,6 +21,140 @@ Change Log ...@@ -21,6 +21,140 @@ Change Log
<h2>Next Version (unreleased)</h2> <h2>Next Version (unreleased)</h2>
<ul> <ul>
<li>PR #1302: Use OpenJDK instead of OracleJDK 10 in Travis builds due to Travis problem
</li>
<li>Issue #1032: Error when executing "SELECT DISTINCT ON"
</li>
<li>Issue #1295: ConditionInSelect violates requirements of LocalResult
</li>
<li>PR #1296: Assorted minor changes
</li>
<li>PR #1293: Move HELP and SHOW tests into own files
</li>
<li>PR #1291: Fix update count for REPLACE and move some SQL tests into separate files
</li>
<li>PR #1290: Do not load the whole LOBs into memory for comparison operation
</li>
<li>Issue #408: DISTINCT does not properly work with ORDER BY on function like LOWER
</li>
<li>PR #1286: Fix MVTempResult implementations for results with invisible columns
</li>
<li>Issue #1284: Nanoseconds of timestamps from old H2 versions are not read properly
</li>
<li>PR #1283: Clean up interaction between LocalResult and ResultExternal
</li>
<li>Issue #1265: OOME is not handled properly in TraceObject.logAndConvert()
</li>
<li>Issue #1061: Regression: Braces after WITH clause not allowed anymore
</li>
<li>PR #1277: Assorted changes in Parser
</li>
<li>PR #1276: Improve support of ARRAY and SQLXML in JDBC layer
</li>
<li>PR #1275: Do not quote other lower case characters
</li>
<li>PR #1274: Use token type in Parser instead of string comparisons
</li>
<li>PR #1272: Reduce code duplication in Parser
</li>
<li>PR #1271: Minor memory leak
</li>
<li>PR #1270: drop TableView isPersistent field
</li>
<li>PR #1269: Eliminate commit of empty batch in some tests
</li>
<li>Issue #1266: Add INFORMATION_SCHEMA.COLUMNS.DATETIME_PRECISION
</li>
<li>Issue #1261: How to discover stored enum types through INFORMATION_SCHEMA
</li>
<li>Issue #1258: Failing to remove index when using schema.table
</li>
<li>PR #1256: misc tiny refactorings
</li>
<li>PR #1255: Minor changes in MERGE USING, DATE_TRUNC, and EXTRACT
</li>
<li>Issue #1214: Internal compiler believes that "3 warnings" is an error
</li>
<li>PR #1252: Assorted minor changes
</li>
<li>PR #1251: Fix SQL representation of CAST for types with fractional seconds precision
</li>
<li>PR #1250: Batch append mode for MVMap
</li>
<li>PR #1248: StringIndexOutOfBoundsException due to undoLog map
</li>
<li>PR #1246: Detect disabled tests
</li>
<li>PR #1242: Add implementation of SQLXML interface
</li>
<li>PR #1241: Various tweaks in attempting to fix TestDiskFull test
</li>
<li>PR #1240: Optimise ValueLobDB comparison methods
</li>
<li>PR #1239: Don't try to find tools.jar on Java 9+
</li>
<li>PR #1238: remove unfinished android API
</li>
<li>PR #1237: remove JaQu
</li>
<li>PR #1236: remove STORE_LOCAL_TIME code
</li>
<li>PR #1235: Do not use deprecated Class.newInstance()
</li>
<li>PR #1234: Fix NPE in Parser.parseMergeUsing()
</li>
<li>PR #1233: Simplify old lob ValueLob class
</li>
<li>Issue 1227: lob growth in pagestore mode
</li>
<li>PR #1230: clean up some javadoc and some throws clauses
</li>
<li>PR #1229: Create UndoLog only when necessary and remove outdated code
</li>
<li>PR #1228: Remove some PageStore+MVCC leftovers
</li>
<li>PR #1226: Fix inconsistencies in checks for transaction isolation level
</li>
<li>PR #1224: Enable Java 10 testing on Travis
</li>
<li>PR #1223: Fix issues with testing on latest Java versions
</li>
<li>PR #1222: Leftovers handling
</li>
<li>Issue #1220: JDK-9 build fails due to usage of java.xml.bind in external authentication
</li>
<li>PR #1218: Test utilities only once during TestAll
</li>
<li>PR #1217: Postpone session.endStatement() until after commit
</li>
<li>PR #1213: KillRestart fix
</li>
<li>PR #1211: Assorted minor changes
</li>
<li>Issue #1204: Always use MVCC with MVStore and never use it with PageStore
</li>
<li>PR #1206: Forbid reconnects in non-regular modes in TestScript
</li>
<li>PR #1205: Misc test fixes
</li>
<li>Issue 1198: Enable MULTI_THREADED by default for MVStore mode
</li>
<li>Issue #1195: Calling setBytes to set VARCHAR field fails
</li>
<li>PR #1197: Fix or suppress errors in tests
</li>
<li>PR #1194: TestKillRestartMulti: A map named undoLog-1 already exists
</li>
<li>PR #1193: enable TestRandomSQL on non-memory databases
</li>
<li>PR #1191: External authentication with datasource issue
</li>
<li>PR #1188: Undo log split to reduce contention
</li>
<li>PR #1186: TransactionMap::sizeAsLong() optimized - temp map eliminated
</li>
<li>PR #1185: Improve naming of the object id field in Prepared
</li>
<li>Issue #1196: Feature request for MS SQL Server Compatibility Mode <li>Issue #1196: Feature request for MS SQL Server Compatibility Mode
</li> </li>
<li>Issue #1177: Resource leak in Recover tool <li>Issue #1177: Resource leak in Recover tool
......
...@@ -152,7 +152,7 @@ ...@@ -152,7 +152,7 @@
90119=User data type {0} already exists 90119=User data type {0} already exists
90120=User data type {0} not found 90120=User data type {0} not found
90121=Database is already closed (to disable automatic closing at VM shutdown, add ";DB_CLOSE_ON_EXIT=FALSE" to the db URL) 90121=Database is already closed (to disable automatic closing at VM shutdown, add ";DB_CLOSE_ON_EXIT=FALSE" to the db URL)
90122=Operation not supported for table {0} when there are views on the table: {1} 90122=The WITH TIES clause is not allowed without a corresponding ORDER BY clause.
90123=Cannot mix indexed and non-indexed parameters 90123=Cannot mix indexed and non-indexed parameters
90124=File not found: {0} 90124=File not found: {0}
90125=Invalid class, expected {0} but got {1} 90125=Invalid class, expected {0} but got {1}
......
...@@ -1756,6 +1756,12 @@ public class ErrorCode { ...@@ -1756,6 +1756,12 @@ public class ErrorCode {
*/ */
public static final int DATABASE_CALLED_AT_SHUTDOWN = 90121; public static final int DATABASE_CALLED_AT_SHUTDOWN = 90121;
/**
* The error with code <code>90122</code> is thrown when
* WITH TIES clause is used without ORDER BY clause.
*/
public static final int WITH_TIES_WITHOUT_ORDER_BY = 90122;
/** /**
* The error with code <code>90123</code> is thrown when * The error with code <code>90123</code> is thrown when
* trying mix regular parameters and indexed parameters in the same * trying mix regular parameters and indexed parameters in the same
...@@ -2002,7 +2008,7 @@ public class ErrorCode { ...@@ -2002,7 +2008,7 @@ public class ErrorCode {
public static final int AUTHENTICATOR_NOT_AVAILABLE = 90144; public static final int AUTHENTICATOR_NOT_AVAILABLE = 90144;
// next are 90122, 90145 // next is 90145
private ErrorCode() { private ErrorCode() {
// utility class // utility class
......
...@@ -2381,8 +2381,13 @@ public class Parser { ...@@ -2381,8 +2381,13 @@ public class Parser {
read("ROWS"); read("ROWS");
} }
} }
if (readIf(WITH)) {
read("TIES");
command.setWithTies(true);
} else {
read("ONLY"); read("ONLY");
} }
}
currentSelect = temp; currentSelect = temp;
if (readIf(LIMIT)) { if (readIf(LIMIT)) {
temp = currentSelect; temp = currentSelect;
...@@ -2527,6 +2532,10 @@ public class Parser { ...@@ -2527,6 +2532,10 @@ public class Parser {
// SELECT TOP 1 (+?) AS A FROM TEST // SELECT TOP 1 (+?) AS A FROM TEST
Expression limit = readTerm().optimize(session); Expression limit = readTerm().optimize(session);
command.setLimit(limit); command.setLimit(limit);
if (readIf(WITH)) {
read("TIES");
command.setWithTies(true);
}
} else if (readIf(LIMIT)) { } else if (readIf(LIMIT)) {
Expression offset = readTerm().optimize(session); Expression offset = readTerm().optimize(session);
command.setOffset(offset); command.setOffset(offset);
...@@ -2535,7 +2544,16 @@ public class Parser { ...@@ -2535,7 +2544,16 @@ public class Parser {
} }
currentSelect = temp; currentSelect = temp;
if (readIf(DISTINCT)) { if (readIf(DISTINCT)) {
command.setDistinct(true); if (readIf(ON)) {
read(OPEN_PAREN);
ArrayList<Expression> distinctExpressions = Utils.newSmallArrayList();
do {
distinctExpressions.add(readExpression());
} while (readIfMore(true));
command.setDistinct(distinctExpressions.toArray(new Expression[0]));
} else {
command.setDistinct();
}
} else { } else {
readIf(ALL); readIf(ALL);
} }
...@@ -2749,9 +2767,7 @@ public class Parser { ...@@ -2749,9 +2767,7 @@ public class Parser {
} else { } else {
if (isSelect()) { if (isSelect()) {
Query query = parseSelect(); Query query = parseSelect();
// can not be lazy because we have to call // TODO lazy result causes timeout in TestFuzzOptimizations
// method ResultInterface.containsDistinct
// which is not supported for lazy execution
query.setNeverLazy(true); query.setNeverLazy(true);
r = new ConditionInSelect(database, r, query, false, r = new ConditionInSelect(database, r, query, false,
Comparison.EQUAL); Comparison.EQUAL);
......
...@@ -7,6 +7,7 @@ package org.h2.command.dml; ...@@ -7,6 +7,7 @@ package org.h2.command.dml;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
import java.util.List;
import org.h2.api.ErrorCode; import org.h2.api.ErrorCode;
import org.h2.command.Prepared; import org.h2.command.Prepared;
...@@ -31,6 +32,7 @@ import org.h2.result.SortOrder; ...@@ -31,6 +32,7 @@ import org.h2.result.SortOrder;
import org.h2.table.ColumnResolver; import org.h2.table.ColumnResolver;
import org.h2.table.Table; import org.h2.table.Table;
import org.h2.table.TableFilter; import org.h2.table.TableFilter;
import org.h2.util.StringUtils;
import org.h2.util.Utils; import org.h2.util.Utils;
import org.h2.value.Value; import org.h2.value.Value;
import org.h2.value.ValueInt; import org.h2.value.ValueInt;
...@@ -46,6 +48,11 @@ public abstract class Query extends Prepared { ...@@ -46,6 +48,11 @@ public abstract class Query extends Prepared {
*/ */
protected Expression limitExpr; protected Expression limitExpr;
/**
* Whether tied rows should be included in result too.
*/
protected boolean withTies;
/** /**
* The offset expression as specified in the LIMIT .. OFFSET clause. * The offset expression as specified in the LIMIT .. OFFSET clause.
*/ */
...@@ -255,14 +262,29 @@ public abstract class Query extends Prepared { ...@@ -255,14 +262,29 @@ public abstract class Query extends Prepared {
/** /**
* Set the distinct flag. * Set the distinct flag.
*
* @param b the new value
*/ */
public void setDistinct(boolean b) { public void setDistinct() {
distinct = b; distinct = true;
} }
public boolean isDistinct() { /**
* Set the distinct flag only if it is possible, may be used as a possible
* optimization only.
*/
public abstract void setDistinctIfPossible();
/**
* @return whether this query is a plain {@code DISTINCT} query
*/
public boolean isStandardDistinct() {
return distinct;
}
/**
* @return whether this query is a {@code DISTINCT} or
* {@code DISTINCT ON (...)} query
*/
public boolean isAnyDistinct() {
return distinct; return distinct;
} }
...@@ -401,16 +423,25 @@ public abstract class Query extends Prepared { ...@@ -401,16 +423,25 @@ public abstract class Query extends Prepared {
static void initOrder(Session session, static void initOrder(Session session,
ArrayList<Expression> expressions, ArrayList<Expression> expressions,
ArrayList<String> expressionSQL, ArrayList<String> expressionSQL,
ArrayList<SelectOrderBy> orderList, List<SelectOrderBy> orderList,
int visible, int visible,
boolean mustBeInResult, boolean mustBeInResult,
ArrayList<TableFilter> filters) { ArrayList<TableFilter> filters) {
Database db = session.getDatabase();
for (SelectOrderBy o : orderList) { for (SelectOrderBy o : orderList) {
Expression e = o.expression; Expression e = o.expression;
if (e == null) { if (e == null) {
continue; continue;
} }
int idx = initExpression(session, expressions, expressionSQL, e, visible, mustBeInResult, filters);
o.columnIndexExpr = ValueExpression.get(ValueInt.get(idx + 1));
o.expression = expressions.get(idx).getNonAliasExpression();
}
}
static int initExpression(Session session, ArrayList<Expression> expressions,
ArrayList<String> expressionSQL, Expression e, int visible, boolean mustBeInResult,
ArrayList<TableFilter> filters) {
Database db = session.getDatabase();
// special case: SELECT 1 AS A FROM DUAL ORDER BY A // special case: SELECT 1 AS A FROM DUAL ORDER BY A
// (oracle supports it, but only in order by, not in group by and // (oracle supports it, but only in order by, not in group by and
// not in having): // not in having):
...@@ -494,9 +525,7 @@ public abstract class Query extends Prepared { ...@@ -494,9 +525,7 @@ public abstract class Query extends Prepared {
String sql = e.getSQL(); String sql = e.getSQL();
expressionSQL.add(sql); expressionSQL.add(sql);
} }
o.columnIndexExpr = ValueExpression.get(ValueInt.get(idx + 1)); return idx;
o.expression = expressions.get(idx).getNonAliasExpression();
}
} }
/** /**
...@@ -621,6 +650,14 @@ public abstract class Query extends Prepared { ...@@ -621,6 +650,14 @@ public abstract class Query extends Prepared {
return limitExpr; return limitExpr;
} }
public void setWithTies(boolean withTies) {
this.withTies = true;
}
public boolean isWithTies() {
return withTies;
}
/** /**
* Add a parameter to the parameter list. * Add a parameter to the parameter list.
* *
...@@ -659,4 +696,22 @@ public abstract class Query extends Prepared { ...@@ -659,4 +696,22 @@ public abstract class Query extends Prepared {
isEverything(visitor); isEverything(visitor);
return visitor.getMaxDataModificationId(); return visitor.getMaxDataModificationId();
} }
void appendLimitToSQL(StringBuilder buff) {
if (limitExpr != null) {
if (withTies) {
if (offsetExpr != null) {
buff.append("\nOFFSET ").append(StringUtils.unEnclose(offsetExpr.getSQL())).append(" ROWS");
}
buff.append("\nFETCH NEXT ").append(StringUtils.unEnclose(limitExpr.getSQL()))
.append(" ROWS WITH TIES");
} else {
buff.append("\nLIMIT ").append(StringUtils.unEnclose(limitExpr.getSQL()));
if (offsetExpr != null) {
buff.append("\nOFFSET ").append(StringUtils.unEnclose(offsetExpr.getSQL()));
}
}
}
}
} }
...@@ -125,6 +125,11 @@ public class SelectUnion extends Query { ...@@ -125,6 +125,11 @@ public class SelectUnion extends Query {
return orderList != null || sort != null; return orderList != null || sort != null;
} }
@Override
public void setDistinctIfPossible() {
setDistinct();
}
private Value[] convert(Value[] values, int columnCount) { private Value[] convert(Value[] values, int columnCount) {
Value[] newValues; Value[] newValues;
if (columnCount == values.length) { if (columnCount == values.length) {
...@@ -210,25 +215,22 @@ public class SelectUnion extends Query { ...@@ -210,25 +215,22 @@ public class SelectUnion extends Query {
result.setSortOrder(sort); result.setSortOrder(sort);
} }
if (distinct) { if (distinct) {
left.setDistinct(true); left.setDistinctIfPossible();
right.setDistinct(true); right.setDistinctIfPossible();
result.setDistinct(); result.setDistinct();
} }
if (randomAccessResult) {
result.setRandomAccess();
}
switch (unionType) { switch (unionType) {
case UNION: case UNION:
case EXCEPT: case EXCEPT:
left.setDistinct(true); left.setDistinctIfPossible();
right.setDistinct(true); right.setDistinctIfPossible();
result.setDistinct(); result.setDistinct();
break; break;
case UNION_ALL: case UNION_ALL:
break; break;
case INTERSECT: case INTERSECT:
left.setDistinct(true); left.setDistinctIfPossible();
right.setDistinct(true); right.setDistinctIfPossible();
break; break;
default: default:
DbException.throwInternalError("type=" + unionType); DbException.throwInternalError("type=" + unionType);
...@@ -260,7 +262,6 @@ public class SelectUnion extends Query { ...@@ -260,7 +262,6 @@ public class SelectUnion extends Query {
case INTERSECT: { case INTERSECT: {
LocalResult temp = new LocalResult(session, expressionArray, columnCount); LocalResult temp = new LocalResult(session, expressionArray, columnCount);
temp.setDistinct(); temp.setDistinct();
temp.setRandomAccess();
while (l.next()) { while (l.next()) {
temp.addRow(convert(l.currentRow(), columnCount)); temp.addRow(convert(l.currentRow(), columnCount));
} }
...@@ -283,6 +284,7 @@ public class SelectUnion extends Query { ...@@ -283,6 +284,7 @@ public class SelectUnion extends Query {
Value v = limitExpr.getValue(session); Value v = limitExpr.getValue(session);
if (v != ValueNull.INSTANCE) { if (v != ValueNull.INSTANCE) {
result.setLimit(v.getInt()); result.setLimit(v.getInt());
result.setWithTies(withTies);
} }
} }
l.close(); l.close();
...@@ -318,6 +320,9 @@ public class SelectUnion extends Query { ...@@ -318,6 +320,9 @@ public class SelectUnion extends Query {
Expression l = le.get(i); Expression l = le.get(i);
expressions.add(l); expressions.add(l);
} }
if (withTies && !hasOrder()) {
throw DbException.get(ErrorCode.WITH_TIES_WITHOUT_ORDER_BY);
}
} }
@Override @Override
...@@ -445,14 +450,7 @@ public class SelectUnion extends Query { ...@@ -445,14 +450,7 @@ public class SelectUnion extends Query {
if (sort != null) { if (sort != null) {
buff.append("\nORDER BY ").append(sort.getSQL(exprList, exprList.length)); buff.append("\nORDER BY ").append(sort.getSQL(exprList, exprList.length));
} }
if (limitExpr != null) { appendLimitToSQL(buff);
buff.append("\nLIMIT ").append(
StringUtils.unEnclose(limitExpr.getSQL()));
if (offsetExpr != null) {
buff.append("\nOFFSET ").append(
StringUtils.unEnclose(offsetExpr.getSQL()));
}
}
if (sampleSizeExpr != null) { if (sampleSizeExpr != null) {
buff.append("\nSAMPLE_SIZE ").append( buff.append("\nSAMPLE_SIZE ").append(
StringUtils.unEnclose(sampleSizeExpr.getSQL())); StringUtils.unEnclose(sampleSizeExpr.getSQL()));
......
...@@ -43,9 +43,7 @@ public class ConditionInSelect extends Condition { ...@@ -43,9 +43,7 @@ public class ConditionInSelect extends Condition {
@Override @Override
public Value getValue(Session session) { public Value getValue(Session session) {
query.setSession(session); query.setSession(session);
if (!query.hasOrder()) { query.setDistinctIfPossible();
query.setDistinct(true);
}
ResultInterface rows = query.query(0); ResultInterface rows = query.query(0);
Value l = left.getValue(session); Value l = left.getValue(session);
if (!rows.hasNext()) { if (!rows.hasNext()) {
......
...@@ -734,7 +734,8 @@ public class JdbcPreparedStatement extends JdbcStatement implements ...@@ -734,7 +734,8 @@ public class JdbcPreparedStatement extends JdbcStatement implements
if (x == null) { if (x == null) {
setParameter(parameterIndex, ValueNull.INSTANCE); setParameter(parameterIndex, ValueNull.INSTANCE);
} else { } else {
setParameter(parameterIndex, DateTimeUtils.convertDate(x, calendar)); setParameter(parameterIndex,
calendar != null ? DateTimeUtils.convertDate(x, calendar) : ValueDate.get(x));
} }
} catch (Exception e) { } catch (Exception e) {
throw logAndConvert(e); throw logAndConvert(e);
...@@ -760,7 +761,8 @@ public class JdbcPreparedStatement extends JdbcStatement implements ...@@ -760,7 +761,8 @@ public class JdbcPreparedStatement extends JdbcStatement implements
if (x == null) { if (x == null) {
setParameter(parameterIndex, ValueNull.INSTANCE); setParameter(parameterIndex, ValueNull.INSTANCE);
} else { } else {
setParameter(parameterIndex, DateTimeUtils.convertTime(x, calendar)); setParameter(parameterIndex,
calendar != null ? DateTimeUtils.convertTime(x, calendar) : ValueTime.get(x));
} }
} catch (Exception e) { } catch (Exception e) {
throw logAndConvert(e); throw logAndConvert(e);
...@@ -787,7 +789,8 @@ public class JdbcPreparedStatement extends JdbcStatement implements ...@@ -787,7 +789,8 @@ public class JdbcPreparedStatement extends JdbcStatement implements
if (x == null) { if (x == null) {
setParameter(parameterIndex, ValueNull.INSTANCE); setParameter(parameterIndex, ValueNull.INSTANCE);
} else { } else {
setParameter(parameterIndex, DateTimeUtils.convertTimestamp(x, calendar)); setParameter(parameterIndex,
calendar != null ? DateTimeUtils.convertTimestamp(x, calendar) : ValueTimestamp.get(x));
} }
} catch (Exception e) { } catch (Exception e) {
throw logAndConvert(e); throw logAndConvert(e);
......
...@@ -2493,7 +2493,6 @@ public class JdbcResultSet extends TraceObject implements ResultSet, JdbcResultS ...@@ -2493,7 +2493,6 @@ public class JdbcResultSet extends TraceObject implements ResultSet, JdbcResultS
* *
* @param columnIndex (1,2,...) * @param columnIndex (1,2,...)
* @param x the value * @param x the value
* @param length the length
* @throws SQLException if the result set is closed or not updatable * @throws SQLException if the result set is closed or not updatable
*/ */
@Override @Override
...@@ -2520,7 +2519,6 @@ public class JdbcResultSet extends TraceObject implements ResultSet, JdbcResultS ...@@ -2520,7 +2519,6 @@ public class JdbcResultSet extends TraceObject implements ResultSet, JdbcResultS
* *
* @param columnLabel the column label * @param columnLabel the column label
* @param x the value * @param x the value
* @param length the length
* @throws SQLException if the result set is closed or not updatable * @throws SQLException if the result set is closed or not updatable
*/ */
@Override @Override
...@@ -3930,9 +3928,9 @@ public class JdbcResultSet extends TraceObject implements ResultSet, JdbcResultS ...@@ -3930,9 +3928,9 @@ public class JdbcResultSet extends TraceObject implements ResultSet, JdbcResultS
return type.cast(value == ValueNull.INSTANCE return type.cast(value == ValueNull.INSTANCE
? null : new JdbcSQLXML(conn, value, JdbcLob.State.WITH_VALUE, id)); ? null : new JdbcSQLXML(conn, value, JdbcLob.State.WITH_VALUE, id));
} else if (type == TimestampWithTimeZone.class) { } else if (type == TimestampWithTimeZone.class) {
return type.cast(value.getObject()); return type.cast(value.convertTo(Value.TIMESTAMP_TZ).getObject());
} else if (DataType.isGeometryClass(type)) { } else if (DataType.isGeometryClass(type)) {
return type.cast(value.getObject()); return type.cast(value.convertTo(Value.GEOMETRY).getObject());
} else if (type == LocalDateTimeUtils.LOCAL_DATE) { } else if (type == LocalDateTimeUtils.LOCAL_DATE) {
return type.cast(LocalDateTimeUtils.valueToLocalDate(value)); return type.cast(LocalDateTimeUtils.valueToLocalDate(value));
} else if (type == LocalDateTimeUtils.LOCAL_TIME) { } else if (type == LocalDateTimeUtils.LOCAL_TIME) {
......
...@@ -1418,7 +1418,7 @@ public class MVMap<K, V> extends AbstractMap<K, V> ...@@ -1418,7 +1418,7 @@ public class MVMap<K, V> extends AbstractMap<K, V>
this.appendCounter = 0; this.appendCounter = 0;
} }
// This one is used for append buffer maintance // This one is used for append buffer maintenance
RootReference(RootReference r, int appendCounter, int attempt) { RootReference(RootReference r, int appendCounter, int attempt) {
this.root = r.root; this.root = r.root;
this.version = r.version; this.version = r.version;
......
...@@ -5,8 +5,6 @@ ...@@ -5,8 +5,6 @@
*/ */
package org.h2.mvstore.db; package org.h2.mvstore.db;
import java.util.Arrays;
import org.h2.engine.Database; import org.h2.engine.Database;
import org.h2.expression.Expression; import org.h2.expression.Expression;
import org.h2.message.DbException; import org.h2.message.DbException;
...@@ -22,11 +20,6 @@ import org.h2.value.ValueArray; ...@@ -22,11 +20,6 @@ import org.h2.value.ValueArray;
*/ */
class MVPlainTempResult extends MVTempResult { class MVPlainTempResult extends MVTempResult {
/**
* The type of the distinct values.
*/
private final ValueDataType distinctType;
/** /**
* Map with identities of rows as keys rows as values. * Map with identities of rows as keys rows as values.
*/ */
...@@ -39,12 +32,6 @@ class MVPlainTempResult extends MVTempResult { ...@@ -39,12 +32,6 @@ class MVPlainTempResult extends MVTempResult {
*/ */
private long counter; private long counter;
/**
* Optional index. This index is created only if {@link #contains(Value[])}
* method is invoked. Only the root result should have an index if required.
*/
private MVMap<ValueArray, Boolean> index;
/** /**
* Cursor for the {@link #next()} method. * Cursor for the {@link #next()} method.
*/ */
...@@ -58,7 +45,6 @@ class MVPlainTempResult extends MVTempResult { ...@@ -58,7 +45,6 @@ class MVPlainTempResult extends MVTempResult {
*/ */
private MVPlainTempResult(MVPlainTempResult parent) { private MVPlainTempResult(MVPlainTempResult parent) {
super(parent); super(parent);
this.distinctType = null;
this.map = parent.map; this.map = parent.map;
} }
...@@ -75,46 +61,20 @@ class MVPlainTempResult extends MVTempResult { ...@@ -75,46 +61,20 @@ class MVPlainTempResult extends MVTempResult {
MVPlainTempResult(Database database, Expression[] expressions, int visibleColumnCount) { MVPlainTempResult(Database database, Expression[] expressions, int visibleColumnCount) {
super(database, expressions.length, visibleColumnCount); super(database, expressions.length, visibleColumnCount);
ValueDataType valueType = new ValueDataType(database.getCompareMode(), database, new int[columnCount]); ValueDataType valueType = new ValueDataType(database.getCompareMode(), database, new int[columnCount]);
if (columnCount == visibleColumnCount) {
distinctType = valueType;
} else {
distinctType = new ValueDataType(database.getCompareMode(), database, new int[visibleColumnCount]);
}
Builder<Long, ValueArray> builder = new MVMap.Builder<Long, ValueArray>().valueType(valueType); Builder<Long, ValueArray> builder = new MVMap.Builder<Long, ValueArray>().valueType(valueType);
map = store.openMap("tmp", builder); map = store.openMap("tmp", builder);
} }
@Override @Override
public int addRow(Value[] values) { public int addRow(Value[] values) {
assert parent == null && index == null; assert parent == null;
map.put(counter++, ValueArray.get(values)); map.put(counter++, ValueArray.get(values));
return ++rowCount; return ++rowCount;
} }
@Override @Override
public boolean contains(Value[] values) { public boolean contains(Value[] values) {
// Only parent result maintains the index throw DbException.getUnsupportedException("contains()");
if (parent != null) {
return parent.contains(values);
}
if (index == null) {
createIndex();
}
return index.containsKey(ValueArray.get(values));
}
private void createIndex() {
Builder<ValueArray, Boolean> builder = new MVMap.Builder<ValueArray, Boolean>().keyType(distinctType);
index = store.openMap("idx", builder);
Cursor<Long, ValueArray> c = map.cursor(null);
while (c.hasNext()) {
c.next();
ValueArray row = c.getValue();
if (columnCount != visibleColumnCount) {
row = ValueArray.get(Arrays.copyOf(row.getList(), visibleColumnCount));
}
index.putIfAbsent(row, true);
}
} }
@Override @Override
......
...@@ -29,10 +29,15 @@ import org.h2.value.ValueArray; ...@@ -29,10 +29,15 @@ import org.h2.value.ValueArray;
class MVSortedTempResult extends MVTempResult { class MVSortedTempResult extends MVTempResult {
/** /**
* Whether this result is distinct. * Whether this result is a standard distinct result.
*/ */
private final boolean distinct; private final boolean distinct;
/**
* Distinct indexes for DISTINCT ON results.
*/
private final int[] distinctIndexes;
/** /**
* Mapping of indexes of columns to its positions in the store, or {@code null} * Mapping of indexes of columns to its positions in the store, or {@code null}
* if columns are not reordered. * if columns are not reordered.
...@@ -45,11 +50,6 @@ class MVSortedTempResult extends MVTempResult { ...@@ -45,11 +50,6 @@ class MVSortedTempResult extends MVTempResult {
*/ */
private final MVMap<ValueArray, Long> map; private final MVMap<ValueArray, Long> map;
/**
* The type of the distinct values.
*/
private final ValueDataType distinctType;
/** /**
* Optional index. This index is created only if result is distinct and * Optional index. This index is created only if result is distinct and
* {@code columnCount != distinctColumnCount} or if * {@code columnCount != distinctColumnCount} or if
...@@ -84,9 +84,9 @@ class MVSortedTempResult extends MVTempResult { ...@@ -84,9 +84,9 @@ class MVSortedTempResult extends MVTempResult {
private MVSortedTempResult(MVSortedTempResult parent) { private MVSortedTempResult(MVSortedTempResult parent) {
super(parent); super(parent);
this.distinct = parent.distinct; this.distinct = parent.distinct;
this.distinctIndexes = parent.distinctIndexes;
this.indexes = parent.indexes; this.indexes = parent.indexes;
this.map = parent.map; this.map = parent.map;
this.distinctType = null;
this.rowCount = parent.rowCount; this.rowCount = parent.rowCount;
} }
...@@ -99,16 +99,19 @@ class MVSortedTempResult extends MVTempResult { ...@@ -99,16 +99,19 @@ class MVSortedTempResult extends MVTempResult {
* column expressions * column expressions
* @param distinct * @param distinct
* whether this result should be distinct * whether this result should be distinct
* @param distinctIndexes
* indexes of distinct columns for DISTINCT ON results
* @param visibleColumnCount * @param visibleColumnCount
* count of visible columns * count of visible columns
* @param sort * @param sort
* sort order, or {@code null} if this result does not need any * sort order, or {@code null} if this result does not need any
* sorting * sorting
*/ */
MVSortedTempResult(Database database, Expression[] expressions, boolean distinct, int visibleColumnCount, MVSortedTempResult(Database database, Expression[] expressions, boolean distinct, int[] distinctIndexes,
SortOrder sort) { int visibleColumnCount, SortOrder sort) {
super(database, expressions.length, visibleColumnCount); super(database, expressions.length, visibleColumnCount);
this.distinct = distinct; this.distinct = distinct;
this.distinctIndexes = distinctIndexes;
int length = columnCount; int length = columnCount;
int[] sortTypes = new int[length]; int[] sortTypes = new int[length];
int[] indexes; int[] indexes;
...@@ -166,13 +169,11 @@ class MVSortedTempResult extends MVTempResult { ...@@ -166,13 +169,11 @@ class MVSortedTempResult extends MVTempResult {
ValueDataType keyType = new ValueDataType(database.getCompareMode(), database, sortTypes); ValueDataType keyType = new ValueDataType(database.getCompareMode(), database, sortTypes);
Builder<ValueArray, Long> builder = new MVMap.Builder<ValueArray, Long>().keyType(keyType); Builder<ValueArray, Long> builder = new MVMap.Builder<ValueArray, Long>().keyType(keyType);
map = store.openMap("tmp", builder); map = store.openMap("tmp", builder);
if (length == visibleColumnCount) { if (distinct && length != visibleColumnCount || distinctIndexes != null) {
distinctType = null; int count = distinctIndexes != null ? distinctIndexes.length : visibleColumnCount;
} else { ValueDataType distinctType = new ValueDataType(database.getCompareMode(), database, new int[count]);
distinctType = new ValueDataType(database.getCompareMode(), database, new int[visibleColumnCount]); Builder<ValueArray, Boolean> indexBuilder = new MVMap.Builder<ValueArray, Boolean>().keyType(distinctType);
if (distinct) { index = store.openMap("idx", indexBuilder);
createIndex(false);
}
} }
} }
...@@ -180,8 +181,18 @@ class MVSortedTempResult extends MVTempResult { ...@@ -180,8 +181,18 @@ class MVSortedTempResult extends MVTempResult {
public int addRow(Value[] values) { public int addRow(Value[] values) {
assert parent == null; assert parent == null;
ValueArray key = getKey(values); ValueArray key = getKey(values);
if (distinct) { if (distinct || distinctIndexes != null) {
if (columnCount != visibleColumnCount) { if (distinctIndexes != null) {
int cnt = distinctIndexes.length;
Value[] newValues = new Value[cnt];
for (int i = 0; i < cnt; i++) {
newValues[i] = values[distinctIndexes[i]];
}
ValueArray distinctRow = ValueArray.get(newValues);
if (index.putIfAbsent(distinctRow, true) != null) {
return rowCount;
}
} else if (columnCount != visibleColumnCount) {
ValueArray distinctRow = ValueArray.get(Arrays.copyOf(values, visibleColumnCount)); ValueArray distinctRow = ValueArray.get(Arrays.copyOf(values, visibleColumnCount));
if (index.putIfAbsent(distinctRow, true) != null) { if (index.putIfAbsent(distinctRow, true) != null) {
return rowCount; return rowCount;
...@@ -209,29 +220,13 @@ class MVSortedTempResult extends MVTempResult { ...@@ -209,29 +220,13 @@ class MVSortedTempResult extends MVTempResult {
if (parent != null) { if (parent != null) {
return parent.contains(values); return parent.contains(values);
} }
assert distinct;
if (columnCount != visibleColumnCount) { if (columnCount != visibleColumnCount) {
if (index == null) {
createIndex(true);
}
return index.containsKey(ValueArray.get(values)); return index.containsKey(ValueArray.get(values));
} }
return map.containsKey(getKey(values)); return map.containsKey(getKey(values));
} }
private void createIndex(boolean fill) {
Builder<ValueArray, Boolean> indexBuilder = new MVMap.Builder<ValueArray, Boolean>()
.keyType(distinctType);
index = store.openMap("idx", indexBuilder);
if (fill) {
Cursor<ValueArray, Long> c = map.cursor(null);
while (c.hasNext()) {
Value[] v = getValue(c.next().getList());
ValueArray distinctRow = ValueArray.get(Arrays.copyOf(v, visibleColumnCount));
index.putIfAbsent(distinctRow, true);
}
}
}
@Override @Override
public synchronized ResultExternal createShallowCopy() { public synchronized ResultExternal createShallowCopy() {
if (parent != null) { if (parent != null) {
......
...@@ -66,6 +66,8 @@ public abstract class MVTempResult implements ResultExternal { ...@@ -66,6 +66,8 @@ public abstract class MVTempResult implements ResultExternal {
* expressions * expressions
* @param distinct * @param distinct
* is output distinct * is output distinct
* @param distinctIndexes
* indexes of distinct columns for DISTINCT ON results
* @param visibleColumnCount * @param visibleColumnCount
* count of visible columns * count of visible columns
* @param sort * @param sort
...@@ -73,9 +75,9 @@ public abstract class MVTempResult implements ResultExternal { ...@@ -73,9 +75,9 @@ public abstract class MVTempResult implements ResultExternal {
* @return temporary result * @return temporary result
*/ */
public static ResultExternal of(Database database, Expression[] expressions, boolean distinct, public static ResultExternal of(Database database, Expression[] expressions, boolean distinct,
int visibleColumnCount, SortOrder sort) { int[] distinctIndexes, int visibleColumnCount, SortOrder sort) {
return distinct || sort != null return distinct || distinctIndexes != null || sort != null
? new MVSortedTempResult(database, expressions, distinct, visibleColumnCount, sort) ? new MVSortedTempResult(database, expressions, distinct, distinctIndexes, visibleColumnCount, sort)
: new MVPlainTempResult(database, expressions, visibleColumnCount); : new MVPlainTempResult(database, expressions, visibleColumnCount);
} }
......
...@@ -152,7 +152,7 @@ ...@@ -152,7 +152,7 @@
90119=Uživatelský datový typ {0} již existuje 90119=Uživatelský datový typ {0} již existuje
90120=Uživatelský datový typ {0} nenalezen 90120=Uživatelský datový typ {0} nenalezen
90121=Databáze byla již ukončena (pro deaktivaci automatického ukončení při zastavení virtuálního stroje přidejte parametr ";DB_CLOSE_ON_EXIT=FALSE" do URL databáze) 90121=Databáze byla již ukončena (pro deaktivaci automatického ukončení při zastavení virtuálního stroje přidejte parametr ";DB_CLOSE_ON_EXIT=FALSE" do URL databáze)
90122=Operace není podporována pro tabulku {0}, pokud na tabulku existují pohledy: {1} 90122=#The WITH TIES clause is not allowed without a corresponding ORDER BY clause.
90123=Nelze vzájemně míchat indexované a neindexované parametry 90123=Nelze vzájemně míchat indexované a neindexované parametry
90124=Soubor nenalezen: {0} 90124=Soubor nenalezen: {0}
90125=Neplatná třída, očekáváno {0}, ale obdrženo {1} 90125=Neplatná třída, očekáváno {0}, ale obdrženo {1}
......
...@@ -152,7 +152,7 @@ ...@@ -152,7 +152,7 @@
90119=Benutzer-Datentyp {0} besteht bereits 90119=Benutzer-Datentyp {0} besteht bereits
90120=Benutzer-Datentyp {0} nicht gefunden 90120=Benutzer-Datentyp {0} nicht gefunden
90121=Die Datenbank wurde bereits geschlossen (um das automatische Schliessen beim Stopp der VM zu deaktivieren, die Datenbank URL mit ";DB_CLOSE_ON_EXIT=FALSE" ergänzen) 90121=Die Datenbank wurde bereits geschlossen (um das automatische Schliessen beim Stopp der VM zu deaktivieren, die Datenbank URL mit ";DB_CLOSE_ON_EXIT=FALSE" ergänzen)
90122=Funktion nicht unterstützt für Tabelle {0} wenn Views auf die Tabelle vorhanden sind: {1} 90122=#The WITH TIES clause is not allowed without a corresponding ORDER BY clause.
90123=Kann nicht indizierte und nicht indizierte Parameter mischen 90123=Kann nicht indizierte und nicht indizierte Parameter mischen
90124=Datei nicht gefunden: {0} 90124=Datei nicht gefunden: {0}
90125=Ungültig Klasse, erwartet {0} erhalten {1} 90125=Ungültig Klasse, erwartet {0} erhalten {1}
......
...@@ -152,7 +152,7 @@ ...@@ -152,7 +152,7 @@
90119=User data type {0} already exists 90119=User data type {0} already exists
90120=User data type {0} not found 90120=User data type {0} not found
90121=Database is already closed (to disable automatic closing at VM shutdown, add ";DB_CLOSE_ON_EXIT=FALSE" to the db URL) 90121=Database is already closed (to disable automatic closing at VM shutdown, add ";DB_CLOSE_ON_EXIT=FALSE" to the db URL)
90122=Operation not supported for table {0} when there are views on the table: {1} 90122=The WITH TIES clause is not allowed without a corresponding ORDER BY clause.
90123=Cannot mix indexed and non-indexed parameters 90123=Cannot mix indexed and non-indexed parameters
90124=File not found: {0} 90124=File not found: {0}
90125=Invalid class, expected {0} but got {1} 90125=Invalid class, expected {0} but got {1}
......
...@@ -152,7 +152,7 @@ ...@@ -152,7 +152,7 @@
90119=Tipo de dato de usuario {0} ya existe 90119=Tipo de dato de usuario {0} ya existe
90120=Tipo de dato de usuario {0} no encontrado 90120=Tipo de dato de usuario {0} no encontrado
90121=La base de datos ya esta cerrada (para des-habilitar el cerrado automatico durante el shutdown de la VM, agregue ";DB_CLOSE_ON_EXIT=FALSE" a la URL de conexión) 90121=La base de datos ya esta cerrada (para des-habilitar el cerrado automatico durante el shutdown de la VM, agregue ";DB_CLOSE_ON_EXIT=FALSE" a la URL de conexión)
90122=Operación no soportada para la tabla {0} cuando existen vistas sobre la tabla: {1} 90122=#The WITH TIES clause is not allowed without a corresponding ORDER BY clause.
90123=No se puede mezclar parametros indexados y no-indexados 90123=No se puede mezclar parametros indexados y no-indexados
90124=Archivo no encontrado: {0} 90124=Archivo no encontrado: {0}
90125=Clase Invalida, se esperaba {0} pero se obtuvo {1} 90125=Clase Invalida, se esperaba {0} pero se obtuvo {1}
......
...@@ -152,7 +152,7 @@ ...@@ -152,7 +152,7 @@
90119=Le type de données utilisateur {0} existe déjà 90119=Le type de données utilisateur {0} existe déjà
90120=Type de données utilisateur {0} non trouvé 90120=Type de données utilisateur {0} non trouvé
90121=La base de données est déjà fermée (pour désactiver la fermeture automatique à l'arrêt de la VM, ajoutez "; DB_CLOSE_ON_EXIT = FALSE" à l'URL db) 90121=La base de données est déjà fermée (pour désactiver la fermeture automatique à l'arrêt de la VM, ajoutez "; DB_CLOSE_ON_EXIT = FALSE" à l'URL db)
90122=Opération non prise en charge pour la table {0} lorsqu'il existe des vues sur la table: {1} 90122=#The WITH TIES clause is not allowed without a corresponding ORDER BY clause.
90123=Impossible de mélanger des paramètres indexés et non-indexés 90123=Impossible de mélanger des paramètres indexés et non-indexés
90124=Fichier non trouvé: {0} 90124=Fichier non trouvé: {0}
90125=Classe invalide, attendue {0} mais obtenue {1} 90125=Classe invalide, attendue {0} mais obtenue {1}
......
...@@ -152,7 +152,7 @@ ...@@ -152,7 +152,7 @@
90119=ユーザデータ型 {0} はすでに存在します 90119=ユーザデータ型 {0} はすでに存在します
90120=ユーザデータ型 {0} が見つかりません 90120=ユーザデータ型 {0} が見つかりません
90121=データベースはすでに閉じられています (VM終了時の自動データベースクローズを無効にするためには、db URLに ";DB_CLOSE_ON_EXIT=FALSE" を追加してください) 90121=データベースはすでに閉じられています (VM終了時の自動データベースクローズを無効にするためには、db URLに ";DB_CLOSE_ON_EXIT=FALSE" を追加してください)
90122=ビューが存在するテーブル {0} に対する操作はサポートされていません: {1} 90122=#The WITH TIES clause is not allowed without a corresponding ORDER BY clause.
90123=インデックスの付いたパラメータと付いていないパラメータを混在させることはできません 90123=インデックスの付いたパラメータと付いていないパラメータを混在させることはできません
90124=ファイルが見つかりません: {0} 90124=ファイルが見つかりません: {0}
90125=無効なクラス, {0} が期待されているにもかかわらず {1} を取得しました 90125=無効なクラス, {0} が期待されているにもかかわらず {1} を取得しました
......
...@@ -152,7 +152,7 @@ ...@@ -152,7 +152,7 @@
90119=Typ danych użytkownika {0} już istnieje 90119=Typ danych użytkownika {0} już istnieje
90120=Typ danych użytkownika {0} nie istnieje 90120=Typ danych użytkownika {0} nie istnieje
90121=Baza danych jest już zamknięta (aby zablokować samoczynne zamykanie podczas zamknięcia VM dodaj ";DB_CLOSE_ON_EXIT=FALSE" do URL bazy danych) 90121=Baza danych jest już zamknięta (aby zablokować samoczynne zamykanie podczas zamknięcia VM dodaj ";DB_CLOSE_ON_EXIT=FALSE" do URL bazy danych)
90122=Operacja nie jest dozwolona dla tabeli {0} gdy istnieją widoki oparte na tabeli: {1} 90122=#The WITH TIES clause is not allowed without a corresponding ORDER BY clause.
90123=Nie można mieszać parametrów indeksowych z nieindeksowymi 90123=Nie można mieszać parametrów indeksowych z nieindeksowymi
90124=Plik nie istnieje: {0} 90124=Plik nie istnieje: {0}
90125=Nieprawidłowa klasa, oczekiwano {0}, a jest {1} 90125=Nieprawidłowa klasa, oczekiwano {0}, a jest {1}
......
...@@ -152,7 +152,7 @@ ...@@ -152,7 +152,7 @@
90119=Tipo de dados do usuário {0} já existe 90119=Tipo de dados do usuário {0} já existe
90120=Tipo de dados do usuário {0} não foram encontrados 90120=Tipo de dados do usuário {0} não foram encontrados
90121=Base de dados já está fechada (para desabilitar o fechamento automático quando a VM terminar, addicione ";DB_CLOSE_ON_EXIT=FALSE" na url da base de dados) 90121=Base de dados já está fechada (para desabilitar o fechamento automático quando a VM terminar, addicione ";DB_CLOSE_ON_EXIT=FALSE" na url da base de dados)
90122=Operação não suportada para a tabela {0} quando existe alguma vista sobre a tabela: {1} 90122=#The WITH TIES clause is not allowed without a corresponding ORDER BY clause.
90123=Não pode combinar parâmetros de índices com não índices 90123=Não pode combinar parâmetros de índices com não índices
90124=Arquivo não encontrado: {0} 90124=Arquivo não encontrado: {0}
90125=Classe inválida, experada {0} mas está {1} 90125=Classe inválida, experada {0} mas está {1}
......
...@@ -152,7 +152,7 @@ ...@@ -152,7 +152,7 @@
90119=Объект с именем {0} уже существует 90119=Объект с именем {0} уже существует
90120=Домен {0} не найден 90120=Домен {0} не найден
90121=База данных уже закрыта (чтобы отключить автоматическое закрытие базы данных при останове JVM, добавьте ";DB_CLOSE_ON_EXIT=FALSE" в URL) 90121=База данных уже закрыта (чтобы отключить автоматическое закрытие базы данных при останове JVM, добавьте ";DB_CLOSE_ON_EXIT=FALSE" в URL)
90122=Операция для таблицы {0} не поддерживается, пока существуют представления: {1} 90122=Ограничение WITH TIES использовано без соответствующего раздела ORDER BY.
90123=Одновременное использование индексированных и неиндексированных параметров в запросе не поддерживается 90123=Одновременное использование индексированных и неиндексированных параметров в запросе не поддерживается
90124=Файл не найден: {0} 90124=Файл не найден: {0}
90125=Недопустимый класс, ожидался {0}, но получен {1} 90125=Недопустимый класс, ожидался {0}, но получен {1}
......
...@@ -152,7 +152,7 @@ ...@@ -152,7 +152,7 @@
90119=Používateľský dátový typ {0} už existuje 90119=Používateľský dátový typ {0} už existuje
90120=Používateľský dátový typ {0} nenájdený 90120=Používateľský dátový typ {0} nenájdený
90121=Databáza už je zatvorená (na zamedzenie automatického zatvárania pri ukončení VM, pridajte ";DB_CLOSE_ON_EXIT=FALSE" do DB URL) 90121=Databáza už je zatvorená (na zamedzenie automatického zatvárania pri ukončení VM, pridajte ";DB_CLOSE_ON_EXIT=FALSE" do DB URL)
90122=Operácia pre tabuľku {0} nie je podporovaná, kedže existujú na tabuľku pohľady (views): {1} 90122=#The WITH TIES clause is not allowed without a corresponding ORDER BY clause.
90123=Nemožno miešať indexované a neindexované parametre 90123=Nemožno miešať indexované a neindexované parametre
90124=Súbor nenájdený: {0} 90124=Súbor nenájdený: {0}
90125=Nesprávna trieda {1}, očakávana je {0} 90125=Nesprávna trieda {1}, očakávana je {0}
......
...@@ -152,7 +152,7 @@ ...@@ -152,7 +152,7 @@
90119=用户数据类型 {0} 已存在 90119=用户数据类型 {0} 已存在
90120=找不到用户数据类型 {0} 90120=找不到用户数据类型 {0}
90121=数据库已关闭 (若需要禁用在虚拟机关闭的时候同时关闭数据库,请加上 ";DB_CLOSE_ON_EXIT=FALSE" 到数据库连接的 URL) 90121=数据库已关闭 (若需要禁用在虚拟机关闭的时候同时关闭数据库,请加上 ";DB_CLOSE_ON_EXIT=FALSE" 到数据库连接的 URL)
90122={0}表不支持本操作,因为在表上存在视图: {1} 90122=#The WITH TIES clause is not allowed without a corresponding ORDER BY clause.
90123=不能混合已索引和未索引的参数 90123=不能混合已索引和未索引的参数
90124=文件 找不到: {0} 90124=文件 找不到: {0}
90125=无效的类, 取代找到 {0} 但得到 {1} 90125=无效的类, 取代找到 {0} 但得到 {1}
......
...@@ -41,9 +41,10 @@ public class LocalResult implements ResultInterface, ResultTarget { ...@@ -41,9 +41,10 @@ public class LocalResult implements ResultInterface, ResultTarget {
private Value[] currentRow; private Value[] currentRow;
private int offset; private int offset;
private int limit = -1; private int limit = -1;
private boolean withTies;
private ResultExternal external; private ResultExternal external;
private boolean distinct; private boolean distinct;
private boolean randomAccess; private int[] distinctIndexes;
private boolean closed; private boolean closed;
private boolean containsLobs; private boolean containsLobs;
...@@ -151,7 +152,7 @@ public class LocalResult implements ResultInterface, ResultTarget { ...@@ -151,7 +152,7 @@ public class LocalResult implements ResultInterface, ResultTarget {
copy.sort = this.sort; copy.sort = this.sort;
copy.distinctRows = this.distinctRows; copy.distinctRows = this.distinctRows;
copy.distinct = distinct; copy.distinct = distinct;
copy.randomAccess = randomAccess; copy.distinctIndexes = distinctIndexes;
copy.currentRow = null; copy.currentRow = null;
copy.offset = 0; copy.offset = 0;
copy.limit = -1; copy.limit = -1;
...@@ -172,15 +173,27 @@ public class LocalResult implements ResultInterface, ResultTarget { ...@@ -172,15 +173,27 @@ public class LocalResult implements ResultInterface, ResultTarget {
* Remove duplicate rows. * Remove duplicate rows.
*/ */
public void setDistinct() { public void setDistinct() {
assert distinctIndexes == null;
distinct = true; distinct = true;
distinctRows = ValueHashMap.newInstance(); distinctRows = ValueHashMap.newInstance();
} }
/** /**
* Random access is required (containsDistinct). * Remove rows with duplicates in columns with specified indexes.
*
* @param distinctIndexes distinct indexes
*/
public void setDistinct(int[] distinctIndexes) {
assert !distinct;
this.distinctIndexes = distinctIndexes;
distinctRows = ValueHashMap.newInstance();
}
/**
* @return whether this result is a distinct result
*/ */
public void setRandomAccess() { public boolean isAnyDistinct() {
this.randomAccess = true; return distinct || distinctIndexes != null;
} }
/** /**
...@@ -217,7 +230,7 @@ public class LocalResult implements ResultInterface, ResultTarget { ...@@ -217,7 +230,7 @@ public class LocalResult implements ResultInterface, ResultTarget {
if (distinctRows == null) { if (distinctRows == null) {
distinctRows = ValueHashMap.newInstance(); distinctRows = ValueHashMap.newInstance();
for (Value[] row : rows) { for (Value[] row : rows) {
ValueArray array = getArrayOfVisible(row); ValueArray array = getArrayOfDistinct(row);
distinctRows.put(array, array.getList()); distinctRows.put(array, array.getList());
} }
} }
...@@ -278,8 +291,15 @@ public class LocalResult implements ResultInterface, ResultTarget { ...@@ -278,8 +291,15 @@ public class LocalResult implements ResultInterface, ResultTarget {
} }
} }
private ValueArray getArrayOfVisible(Value[] values) { private ValueArray getArrayOfDistinct(Value[] values) {
if (values.length > visibleColumnCount) { if (distinctIndexes != null) {
int cnt = distinctIndexes.length;
Value[] newValues = new Value[cnt];
for (int i = 0; i < cnt; i++) {
newValues[i] = values[distinctIndexes[i]];
}
values = newValues;
} else if (values.length > visibleColumnCount) {
values = Arrays.copyOf(values, visibleColumnCount); values = Arrays.copyOf(values, visibleColumnCount);
} }
return ValueArray.get(values); return ValueArray.get(values);
...@@ -288,7 +308,9 @@ public class LocalResult implements ResultInterface, ResultTarget { ...@@ -288,7 +308,9 @@ public class LocalResult implements ResultInterface, ResultTarget {
private void createExternalResult() { private void createExternalResult() {
Database database = session.getDatabase(); Database database = session.getDatabase();
external = database.isMVStore() external = database.isMVStore()
? MVTempResult.of(database, expressions, distinct, visibleColumnCount, sort) || /* not supported by ResultTempTable */ distinct && expressions.length != visibleColumnCount
|| distinctIndexes != null
? MVTempResult.of(database, expressions, distinct, distinctIndexes, visibleColumnCount, sort)
: new ResultTempTable(session, expressions, distinct, sort); : new ResultTempTable(session, expressions, distinct, sort);
} }
...@@ -300,10 +322,10 @@ public class LocalResult implements ResultInterface, ResultTarget { ...@@ -300,10 +322,10 @@ public class LocalResult implements ResultInterface, ResultTarget {
@Override @Override
public void addRow(Value[] values) { public void addRow(Value[] values) {
cloneLobs(values); cloneLobs(values);
if (distinct) { if (isAnyDistinct()) {
if (distinctRows != null) { if (distinctRows != null) {
ValueArray array = getArrayOfVisible(values); ValueArray array = getArrayOfDistinct(values);
distinctRows.put(array, values); distinctRows.putIfAbsent(array, values);
rowCount = distinctRows.size(); rowCount = distinctRows.size();
if (rowCount > maxMemoryRows) { if (rowCount > maxMemoryRows) {
createExternalResult(); createExternalResult();
...@@ -342,12 +364,13 @@ public class LocalResult implements ResultInterface, ResultTarget { ...@@ -342,12 +364,13 @@ public class LocalResult implements ResultInterface, ResultTarget {
if (external != null) { if (external != null) {
addRowsToDisk(); addRowsToDisk();
} else { } else {
if (distinct) { if (isAnyDistinct()) {
rows = distinctRows.values(); rows = distinctRows.values();
} }
if (sort != null) { if (sort != null && limit != 0) {
if (offset > 0 || limit > 0) { boolean withLimit = limit > 0 && !withTies;
sort.sort(rows, offset, limit < 0 ? rows.size() : limit); if (offset > 0 || withLimit) {
sort.sort(rows, offset, withLimit ? limit : rows.size());
} else { } else {
sort.sort(rows); sort.sort(rows);
} }
...@@ -380,8 +403,16 @@ public class LocalResult implements ResultInterface, ResultTarget { ...@@ -380,8 +403,16 @@ public class LocalResult implements ResultInterface, ResultTarget {
rows.clear(); rows.clear();
return; return;
} }
int to = offset + limit;
if (withTies && sort != null) {
Value[] expected = rows.get(to - 1);
while (to < rows.size() && sort.compare(expected, rows.get(to)) == 0) {
to++;
rowCount++;
}
}
// avoid copying the whole array for each row // avoid copying the whole array for each row
rows = new ArrayList<>(rows.subList(offset, offset + limit)); rows = new ArrayList<>(rows.subList(offset, to));
} else { } else {
if (clearAll) { if (clearAll) {
external.close(); external.close();
...@@ -399,12 +430,24 @@ public class LocalResult implements ResultInterface, ResultTarget { ...@@ -399,12 +430,24 @@ public class LocalResult implements ResultInterface, ResultTarget {
while (--offset >= 0) { while (--offset >= 0) {
temp.next(); temp.next();
} }
Value[] row = null;
while (--limit >= 0) { while (--limit >= 0) {
rows.add(temp.next()); row = temp.next();
rows.add(row);
if (rows.size() > maxMemoryRows) { if (rows.size() > maxMemoryRows) {
addRowsToDisk(); addRowsToDisk();
} }
} }
if (withTies && sort != null && row != null) {
Value[] expected = row;
while ((row = temp.next()) != null && sort.compare(expected, row) == 0) {
rows.add(row);
rowCount++;
if (rows.size() > maxMemoryRows) {
addRowsToDisk();
}
}
}
if (external != null) { if (external != null) {
addRowsToDisk(); addRowsToDisk();
} }
...@@ -430,6 +473,13 @@ public class LocalResult implements ResultInterface, ResultTarget { ...@@ -430,6 +473,13 @@ public class LocalResult implements ResultInterface, ResultTarget {
this.limit = limit; this.limit = limit;
} }
/**
* @param withTies whether tied rows should be included in result too
*/
public void setWithTies(boolean withTies) {
this.withTies = withTies;
}
@Override @Override
public boolean needToClose() { public boolean needToClose() {
return external != null; return external != null;
......
...@@ -144,36 +144,7 @@ public class ResultTempTable implements ResultExternal { ...@@ -144,36 +144,7 @@ public class ResultTempTable implements ResultExternal {
closeable = new CloseImpl(session, table); closeable = new CloseImpl(session, table);
fileRef = tempFileDeleter.addFile(closeable, this); fileRef = tempFileDeleter.addFile(closeable, this);
} }
/*
* If ORDER BY or DISTINCT is specified create the index immediately. If
* they are not specified index still may be created later if required
* for IN (SELECT ...) etc.
*/
if (sort != null || distinct) { if (sort != null || distinct) {
getIndex();
}
}
private ResultTempTable(ResultTempTable parent) {
this.parent = parent;
this.columnCount = parent.columnCount;
this.distinct = parent.distinct;
this.session = parent.session;
this.table = parent.table;
this.rowCount = parent.rowCount;
this.sort = parent.sort;
this.tempFileDeleter = null;
this.closeable = null;
this.fileRef = null;
}
private Index getIndex() {
if (parent != null) {
return parent.getIndex();
}
if (index != null) {
return index;
}
IndexColumn[] indexCols; IndexColumn[] indexCols;
if (sort != null) { if (sort != null) {
int[] colIndex = sort.getQueryColumnIndexes(); int[] colIndex = sort.getQueryColumnIndexes();
...@@ -215,6 +186,26 @@ public class ResultTempTable implements ResultExternal { ...@@ -215,6 +186,26 @@ public class ResultTempTable implements ResultExternal {
if (closeable != null) { if (closeable != null) {
closeable.index = index; closeable.index = index;
} }
}
}
private ResultTempTable(ResultTempTable parent) {
this.parent = parent;
this.columnCount = parent.columnCount;
this.distinct = parent.distinct;
this.session = parent.session;
this.table = parent.table;
this.rowCount = parent.rowCount;
this.sort = parent.sort;
this.tempFileDeleter = null;
this.closeable = null;
this.fileRef = null;
}
private Index getIndex() {
if (parent != null) {
return parent.getIndex();
}
return index; return index;
} }
......
...@@ -13,7 +13,6 @@ import java.util.Calendar; ...@@ -13,7 +13,6 @@ import java.util.Calendar;
import java.util.GregorianCalendar; import java.util.GregorianCalendar;
import java.util.TimeZone; import java.util.TimeZone;
import org.h2.engine.Mode; import org.h2.engine.Mode;
import org.h2.message.DbException;
import org.h2.value.Value; import org.h2.value.Value;
import org.h2.value.ValueDate; import org.h2.value.ValueDate;
import org.h2.value.ValueNull; import org.h2.value.ValueNull;
...@@ -278,9 +277,6 @@ public class DateTimeUtils { ...@@ -278,9 +277,6 @@ public class DateTimeUtils {
* @return the date * @return the date
*/ */
public static ValueDate convertDate(Date x, Calendar calendar) { public static ValueDate convertDate(Date x, Calendar calendar) {
if (calendar == null) {
throw DbException.getInvalidValueException("calendar", null);
}
Calendar cal = (Calendar) calendar.clone(); Calendar cal = (Calendar) calendar.clone();
cal.setTimeInMillis(x.getTime()); cal.setTimeInMillis(x.getTime());
long dateValue = dateValueFromCalendar(cal); long dateValue = dateValueFromCalendar(cal);
...@@ -295,9 +291,6 @@ public class DateTimeUtils { ...@@ -295,9 +291,6 @@ public class DateTimeUtils {
* @return the time * @return the time
*/ */
public static ValueTime convertTime(Time x, Calendar calendar) { public static ValueTime convertTime(Time x, Calendar calendar) {
if (calendar == null) {
throw DbException.getInvalidValueException("calendar", null);
}
Calendar cal = (Calendar) calendar.clone(); Calendar cal = (Calendar) calendar.clone();
cal.setTimeInMillis(x.getTime()); cal.setTimeInMillis(x.getTime());
long nanos = nanosFromCalendar(cal); long nanos = nanosFromCalendar(cal);
...@@ -313,9 +306,6 @@ public class DateTimeUtils { ...@@ -313,9 +306,6 @@ public class DateTimeUtils {
*/ */
public static ValueTimestamp convertTimestamp(Timestamp x, public static ValueTimestamp convertTimestamp(Timestamp x,
Calendar calendar) { Calendar calendar) {
if (calendar == null) {
throw DbException.getInvalidValueException("calendar", null);
}
Calendar cal = (Calendar) calendar.clone(); Calendar cal = (Calendar) calendar.clone();
cal.setTimeInMillis(x.getTime()); cal.setTimeInMillis(x.getTime());
long dateValue = dateValueFromCalendar(cal); long dateValue = dateValueFromCalendar(cal);
......
...@@ -66,7 +66,7 @@ public class ValueHashMap<V> extends HashBase { ...@@ -66,7 +66,7 @@ public class ValueHashMap<V> extends HashBase {
if (k != null && k != ValueNull.DELETED) { if (k != null && k != ValueNull.DELETED) {
// skip the checkSizePut so we don't end up // skip the checkSizePut so we don't end up
// accidentally recursing // accidentally recursing
internalPut(k, oldValues[i]); internalPut(k, oldValues[i], false);
} }
} }
} }
...@@ -88,10 +88,21 @@ public class ValueHashMap<V> extends HashBase { ...@@ -88,10 +88,21 @@ public class ValueHashMap<V> extends HashBase {
*/ */
public void put(Value key, V value) { public void put(Value key, V value) {
checkSizePut(); checkSizePut();
internalPut(key, value); internalPut(key, value, false);
} }
private void internalPut(Value key, V value) { /**
* Add a key value pair, values for existing keys are not replaced.
*
* @param key the key
* @param value the new value
*/
public void putIfAbsent(Value key, V value) {
checkSizePut();
internalPut(key, value, true);
}
private void internalPut(Value key, V value, boolean ifAbsent) {
int index = getIndex(key); int index = getIndex(key);
int plus = 1; int plus = 1;
int deleted = -1; int deleted = -1;
...@@ -113,6 +124,9 @@ public class ValueHashMap<V> extends HashBase { ...@@ -113,6 +124,9 @@ public class ValueHashMap<V> extends HashBase {
deleted = index; deleted = index;
} }
} else if (k.equals(key)) { } else if (k.equals(key)) {
if (ifAbsent) {
return;
}
// update existing // update existing
values[index] = value; values[index] = value;
return; return;
......
...@@ -1246,7 +1246,8 @@ public class TestFunctions extends TestDb implements AggregateFunction { ...@@ -1246,7 +1246,8 @@ public class TestFunctions extends TestDb implements AggregateFunction {
Connection conn = getConnection("functions"); Connection conn = getConnection("functions");
Statement stat = conn.createStatement(); Statement stat = conn.createStatement();
stat.execute("CREATE TABLE TEST(S VARCHAR, TS TIMESTAMP, D DATE, T TIME, TZ TIMESTAMP WITH TIME ZONE)"); stat.execute("CREATE TABLE TEST(S VARCHAR, TS TIMESTAMP, D DATE, T TIME, TZ TIMESTAMP WITH TIME ZONE)");
stat.execute("INSERT INTO TEST VALUES ('2010-01-01 10:11:12', '2010-01-01 10:11:12', '2010-01-01', '10:11:12', '2010-01-01 10:11:12Z')"); stat.execute("INSERT INTO TEST VALUES ('2010-01-01 10:11:12', '2010-01-01 10:11:12',"
+ " '2010-01-01', '10:11:12', '2010-01-01 10:11:12Z')");
ResultSetMetaData md = stat.executeQuery("SELECT DATE_TRUNC('HOUR', S), DATE_TRUNC('HOUR', TS)," ResultSetMetaData md = stat.executeQuery("SELECT DATE_TRUNC('HOUR', S), DATE_TRUNC('HOUR', TS),"
+ " DATE_TRUNC('HOUR', D), DATE_TRUNC('HOUR', T), DATE_TRUNC('HOUR', TZ) FROM TEST") + " DATE_TRUNC('HOUR', D), DATE_TRUNC('HOUR', T), DATE_TRUNC('HOUR', TZ) FROM TEST")
.getMetaData(); .getMetaData();
......
...@@ -149,6 +149,13 @@ public class TestSpatial extends TestDb { ...@@ -149,6 +149,13 @@ public class TestSpatial extends TestDb {
new Coordinate(2, 2), new Coordinate(2, 2),
new Coordinate(1, 1) }); new Coordinate(1, 1) });
assertTrue(polygon.equals(rs.getObject(2))); assertTrue(polygon.equals(rs.getObject(2)));
rs.close();
rs = stat.executeQuery("select id, cast(polygon as varchar) from test");
assertTrue(rs.next());
assertEquals(1, rs.getInt(1));
assertEquals("POLYGON ((1 1, 1 2, 2 2, 1 1))", rs.getObject(2));
assertTrue(polygon.equals(rs.getObject(2, Geometry.class)));
rs.close();
rs = stat.executeQuery("select * from test where polygon = " + rs = stat.executeQuery("select * from test where polygon = " +
"'POLYGON ((1 1, 1 2, 2 2, 1 1))'"); "'POLYGON ((1 1, 1 2, 2 2, 1 1))'");
......
...@@ -1497,6 +1497,12 @@ public class TestResultSet extends TestDb { ...@@ -1497,6 +1497,12 @@ public class TestResultSet extends TestDb {
java.sql.Timestamp.valueOf("2107-08-09 10:11:12.131415")); java.sql.Timestamp.valueOf("2107-08-09 10:11:12.131415"));
prep.execute(); prep.execute();
prep.setInt(1, 5);
prep.setDate(2, java.sql.Date.valueOf("2101-02-03"), null);
prep.setTime(3, java.sql.Time.valueOf("14:05:06"), null);
prep.setTimestamp(4, java.sql.Timestamp.valueOf("2107-08-09 10:11:12.131415"), null);
prep.execute();
rs = stat.executeQuery("SELECT * FROM TEST ORDER BY ID"); rs = stat.executeQuery("SELECT * FROM TEST ORDER BY ID");
assertResultSetMeta(rs, 4, assertResultSetMeta(rs, 4,
new String[] { "ID", "D", "T", "TS" }, new String[] { "ID", "D", "T", "TS" },
...@@ -1545,6 +1551,13 @@ public class TestResultSet extends TestDb { ...@@ -1545,6 +1551,13 @@ public class TestResultSet extends TestDb {
assertEquals("14:05:06", rs.getTime("T").toString()); assertEquals("14:05:06", rs.getTime("T").toString());
assertEquals("2101-02-03", rs.getDate("D").toString()); assertEquals("2101-02-03", rs.getDate("D").toString());
rs.next();
assertEquals(5, rs.getInt("ID"));
assertEquals("2107-08-09 10:11:12.131415",
rs.getTimestamp("TS").toString());
assertEquals("14:05:06", rs.getTime("T").toString());
assertEquals("2101-02-03", rs.getDate("D").toString());
assertFalse(rs.next()); assertFalse(rs.next());
stat.execute("DROP TABLE TEST"); stat.execute("DROP TABLE TEST");
} }
......
...@@ -129,7 +129,7 @@ public class TestScript extends TestDb { ...@@ -129,7 +129,7 @@ public class TestScript extends TestDb {
testScript("ddl/" + s + ".sql"); testScript("ddl/" + s + ".sql");
} }
for (String s : new String[] { "error_reporting", "insertIgnore", "merge", "mergeUsing", "replace", for (String s : new String[] { "error_reporting", "insertIgnore", "merge", "mergeUsing", "replace",
"script", "show", "with" }) { "script", "select", "show", "with" }) {
testScript("dml/" + s + ".sql"); testScript("dml/" + s + ".sql");
} }
for (String s : new String[] { "help" }) { for (String s : new String[] { "help" }) {
......
...@@ -103,3 +103,49 @@ DROP TABLE TEST; ...@@ -103,3 +103,49 @@ DROP TABLE TEST;
DROP TABLE TEST2; DROP TABLE TEST2;
> ok > ok
CREATE TABLE TEST(C1 INT, C2 INT, C3 INT, C4 INT, C5 INT);
> ok
INSERT INTO TEST VALUES(1, 2, 3, 4, 5), (1, 2, 3, 6, 7), (2, 1, 4, 8, 9), (3, 4, 5, 1, 1);
> update count: 4
SELECT DISTINCT ON(C1, C2) C1, C2, C3, C4, C5 FROM TEST;
> C1 C2 C3 C4 C5
> -- -- -- -- --
> 1 2 3 4 5
> 2 1 4 8 9
> 3 4 5 1 1
> rows: 3
SELECT DISTINCT ON(C1 + C2) C1, C2, C3, C4, C5 FROM TEST;
> C1 C2 C3 C4 C5
> -- -- -- -- --
> 1 2 3 4 5
> 3 4 5 1 1
> rows: 2
SELECT DISTINCT ON(C1 + C2, C3) C1, C2, C3, C4, C5 FROM TEST;
> C1 C2 C3 C4 C5
> -- -- -- -- --
> 1 2 3 4 5
> 2 1 4 8 9
> 3 4 5 1 1
> rows: 3
SELECT DISTINCT ON(C1) C2 FROM TEST ORDER BY C1;
> C2
> --
> 2
> 1
> 4
> rows (ordered): 3
EXPLAIN SELECT DISTINCT ON(C1) C2 FROM TEST ORDER BY C1;
>> SELECT DISTINCT ON(C1) C2 FROM PUBLIC.TEST /* PUBLIC.TEST.tableScan */ ORDER BY =C1
SELECT DISTINCT ON(C1) C2 FROM TEST ORDER BY C3;
> exception ORDER_BY_NOT_IN_RESULT
DROP TABLE TEST;
> ok
-- Copyright 2004-2018 H2 Group. Multiple-Licensed under the MPL 2.0,
-- and the EPL 1.0 (http://h2database.com/html/license.html).
-- Initial Developer: H2 Group
--
CREATE TABLE TEST(A INT, B INT, C INT);
> ok
INSERT INTO TEST VALUES (1, 1, 1), (1, 1, 2), (1, 1, 3), (1, 2, 1), (1, 2, 2), (1, 2, 3),
(2, 1, 1), (2, 1, 2), (2, 1, 3), (2, 2, 1), (2, 2, 2), (2, 2, 3);
> update count: 12
SELECT * FROM TEST ORDER BY A, B;
> A B C
> - - -
> 1 1 1
> 1 1 2
> 1 1 3
> 1 2 1
> 1 2 2
> 1 2 3
> 2 1 1
> 2 1 2
> 2 1 3
> 2 2 1
> 2 2 2
> 2 2 3
> rows (ordered): 12
SELECT * FROM TEST ORDER BY A, B, C FETCH FIRST 4 ROWS ONLY;
> A B C
> - - -
> 1 1 1
> 1 1 2
> 1 1 3
> 1 2 1
> rows (ordered): 4
SELECT * FROM TEST ORDER BY A, B, C FETCH FIRST 4 ROWS WITH TIES;
> A B C
> - - -
> 1 1 1
> 1 1 2
> 1 1 3
> 1 2 1
> rows: 4
SELECT * FROM TEST ORDER BY A, B FETCH FIRST 4 ROWS WITH TIES;
> A B C
> - - -
> 1 1 1
> 1 1 2
> 1 1 3
> 1 2 1
> 1 2 2
> 1 2 3
> rows: 6
SELECT * FROM TEST ORDER BY A FETCH FIRST 1 ROW WITH TIES;
> A B C
> - - -
> 1 1 1
> 1 1 2
> 1 1 3
> 1 2 1
> 1 2 2
> 1 2 3
> rows: 6
SELECT TOP (1) WITH TIES * FROM TEST ORDER BY A;
> A B C
> - - -
> 1 1 1
> 1 1 2
> 1 1 3
> 1 2 1
> 1 2 2
> 1 2 3
> rows: 6
SELECT * FROM TEST ORDER BY A, B OFFSET 3 ROWS FETCH NEXT 1 ROW WITH TIES;
> A B C
> - - -
> 1 2 1
> 1 2 2
> 1 2 3
> rows: 3
CREATE INDEX TEST_A_IDX ON TEST(A);
> ok
CREATE INDEX TEST_A_B_IDX ON TEST(A, B);
> ok
SELECT * FROM TEST ORDER BY A FETCH FIRST 1 ROW WITH TIES;
> A B C
> - - -
> 1 1 1
> 1 1 2
> 1 1 3
> 1 2 1
> 1 2 2
> 1 2 3
> rows: 6
SELECT * FROM TEST ORDER BY A, B OFFSET 3 ROWS FETCH NEXT 1 ROW WITH TIES;
> A B C
> - - -
> 1 2 1
> 1 2 2
> 1 2 3
> rows: 3
SELECT * FROM TEST FETCH FIRST 1 ROW WITH TIES;
> exception WITH_TIES_WITHOUT_ORDER_BY
(SELECT * FROM TEST) UNION (SELECT 1, 2, 4) ORDER BY A, B OFFSET 3 ROWS FETCH NEXT 1 ROW WITH TIES;
> A B C
> - - -
> 1 2 1
> 1 2 2
> 1 2 3
> 1 2 4
> rows: 4
(SELECT * FROM TEST) UNION (SELECT 1, 2, 4) FETCH NEXT 1 ROW WITH TIES;
> exception WITH_TIES_WITHOUT_ORDER_BY
EXPLAIN SELECT * FROM TEST ORDER BY A, B OFFSET 3 ROWS FETCH NEXT 1 ROW WITH TIES;
>> SELECT TEST.A, TEST.B, TEST.C FROM PUBLIC.TEST /* PUBLIC.TEST_A_B_IDX */ ORDER BY 1, 2 OFFSET 3 ROWS FETCH NEXT 1 ROWS WITH TIES /* index sorted */
DROP TABLE TEST;
> ok
CREATE TABLE TEST(A NUMERIC, B NUMERIC);
> ok
INSERT INTO TEST VALUES (0, 1), (0.0, 2), (0, 3), (1, 4);
> update count: 4
SELECT A, B FROM TEST ORDER BY A FETCH FIRST 1 ROW WITH TIES;
> A B
> --- -
> 0 1
> 0 3
> 0.0 2
> rows: 3
DROP TABLE TEST;
> ok
...@@ -69,7 +69,8 @@ public class TestTimeStampWithTimeZone extends TestDb { ...@@ -69,7 +69,8 @@ public class TestTimeStampWithTimeZone extends TestDb {
assertEquals(1, ts.getMonth()); assertEquals(1, ts.getMonth());
assertEquals(1, ts.getDay()); assertEquals(1, ts.getDay());
assertEquals(15, ts.getTimeZoneOffsetMins()); assertEquals(15, ts.getTimeZoneOffsetMins());
assertEquals(new TimestampWithTimeZone(1008673L, 43200000000000L, (short) 15), ts); TimestampWithTimeZone firstExpected = new TimestampWithTimeZone(1008673L, 43200000000000L, (short) 15);
assertEquals(firstExpected, ts);
if (LocalDateTimeUtils.isJava8DateApiPresent()) { if (LocalDateTimeUtils.isJava8DateApiPresent()) {
assertEquals("1970-01-01T12:00+00:15", rs.getObject(1, assertEquals("1970-01-01T12:00+00:15", rs.getObject(1,
LocalDateTimeUtils.OFFSET_DATE_TIME).toString()); LocalDateTimeUtils.OFFSET_DATE_TIME).toString());
...@@ -125,6 +126,11 @@ public class TestTimeStampWithTimeZone extends TestDb { ...@@ -125,6 +126,11 @@ public class TestTimeStampWithTimeZone extends TestDb {
assertEquals(2014, columnType); assertEquals(2014, columnType);
rs.close(); rs.close();
rs = stat.executeQuery("select cast(t1 as varchar) from test");
assertTrue(rs.next());
assertEquals(firstExpected, rs.getObject(1, TimestampWithTimeZone.class));
stat.close(); stat.close();
conn.close(); conn.close();
} }
......
...@@ -446,7 +446,8 @@ public class TestValue extends TestDb { ...@@ -446,7 +446,8 @@ public class TestValue extends TestDb {
} }
} }
private static int testLobComparisonImpl(DataHandler dh, int type, int size1, int size2, int suffix1, int suffix2) { private static int testLobComparisonImpl(DataHandler dh, int type, int size1, int size2, int suffix1,
int suffix2) {
byte[] bytes1 = new byte[size1]; byte[] bytes1 = new byte[size1];
byte[] bytes2 = new byte[size2]; byte[] bytes2 = new byte[size2];
if (size1 > 0) { if (size1 > 0) {
......
...@@ -784,3 +784,6 @@ ewkt ewkb informations authzpwd realms mappers jaxb realmname configurationfile ...@@ -784,3 +784,6 @@ ewkt ewkb informations authzpwd realms mappers jaxb realmname configurationfile
authenticators appname interrogate metatable barrier preliminary staticuser staticpassword unregistered inquiry authenticators appname interrogate metatable barrier preliminary staticuser staticpassword unregistered inquiry
ldapexample remoteuser assignments djava validators mock relate mapid tighten ldapexample remoteuser assignments djava validators mock relate mapid tighten
retried helpers unclean missed parsers sax myclass suppose mandatory testxml miao ciao retried helpers unclean missed parsers sax myclass suppose mandatory testxml miao ciao
emptied titlecase ask snk dom xif transformer dbf stx stax xof descriptors
inconsistencies discover eliminated violates tweaks postpone leftovers
tied ties
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论