use of org.h2.mvstore.Cursor in project h2database by h2database.
the class IndexCursor method prepare.
/**
* Prepare this index cursor to make a lookup in index.
*
* @param s Session.
* @param indexConditions Index conditions.
*/
public void prepare(Session s, ArrayList<IndexCondition> indexConditions) {
this.session = s;
alwaysFalse = false;
start = end = null;
inList = null;
inColumn = null;
inResult = null;
inResultTested = null;
intersects = null;
// don't use enhanced for loop to avoid creating objects
for (IndexCondition condition : indexConditions) {
if (condition.isAlwaysFalse()) {
alwaysFalse = true;
break;
}
// lookups, each such lookup will perform an own table scan.
if (index.isFindUsingFullTableScan()) {
continue;
}
Column column = condition.getColumn();
if (condition.getCompareType() == Comparison.IN_LIST) {
if (start == null && end == null) {
if (canUseIndexForIn(column)) {
this.inColumn = column;
inList = condition.getCurrentValueList(s);
inListIndex = 0;
}
}
} else if (condition.getCompareType() == Comparison.IN_QUERY) {
if (start == null && end == null) {
if (canUseIndexForIn(column)) {
this.inColumn = column;
inResult = condition.getCurrentResult();
}
}
} else {
Value v = condition.getCurrentValue(s);
boolean isStart = condition.isStart();
boolean isEnd = condition.isEnd();
boolean isIntersects = condition.isSpatialIntersects();
int columnId = column.getColumnId();
if (columnId >= 0) {
IndexColumn idxCol = indexColumns[columnId];
if (idxCol != null && (idxCol.sortType & SortOrder.DESCENDING) != 0) {
// if the index column is sorted the other way, we swap
// end and start NULLS_FIRST / NULLS_LAST is not a
// problem, as nulls never match anyway
boolean temp = isStart;
isStart = isEnd;
isEnd = temp;
}
}
if (isStart) {
start = getSearchRow(start, columnId, v, true);
}
if (isEnd) {
end = getSearchRow(end, columnId, v, false);
}
if (isIntersects) {
intersects = getSpatialSearchRow(intersects, columnId, v);
}
// an X IN(..) condition, unless the X IN condition can use the index.
if ((isStart || isEnd) && !canUseIndexFor(inColumn)) {
inColumn = null;
inList = null;
inResult = null;
}
if (!session.getDatabase().getSettings().optimizeIsNull) {
if (isStart && isEnd) {
if (v == ValueNull.INSTANCE) {
// join on a column=NULL is always false
alwaysFalse = true;
}
}
}
}
}
if (inColumn != null) {
start = table.getTemplateRow();
}
}
use of org.h2.mvstore.Cursor in project h2database by h2database.
the class PageBtreeIndex method findFirstOrLast.
@Override
public Cursor findFirstOrLast(Session session, boolean first) {
if (first) {
// TODO optimization: this loops through NULL elements
Cursor cursor = find(session, null, false, null);
while (cursor.next()) {
SearchRow row = cursor.getSearchRow();
Value v = row.getValue(columnIds[0]);
if (v != ValueNull.INSTANCE) {
return cursor;
}
}
return cursor;
}
PageBtree root = getPage(rootPageId);
PageBtreeCursor cursor = new PageBtreeCursor(session, this, null);
root.last(cursor);
cursor.previous();
// TODO optimization: this loops through NULL elements
do {
SearchRow row = cursor.getSearchRow();
if (row == null) {
break;
}
Value v = row.getValue(columnIds[0]);
if (v != ValueNull.INSTANCE) {
return cursor;
}
} while (cursor.previous());
return cursor;
}
use of org.h2.mvstore.Cursor in project h2database by h2database.
the class Aggregate method getValue.
@Override
public Value getValue(Session session) {
if (select.isQuickAggregateQuery()) {
switch(type) {
case COUNT:
case COUNT_ALL:
Table table = select.getTopTableFilter().getTable();
return ValueLong.get(table.getRowCount(session));
case MIN:
case MAX:
{
boolean first = type == AggregateType.MIN;
Index index = getMinMaxColumnIndex();
int sortType = index.getIndexColumns()[0].sortType;
if ((sortType & SortOrder.DESCENDING) != 0) {
first = !first;
}
Cursor cursor = index.findFirstOrLast(session, first);
SearchRow row = cursor.getSearchRow();
Value v;
if (row == null) {
v = ValueNull.INSTANCE;
} else {
v = row.getValue(index.getColumns()[0].getColumnId());
}
return v;
}
case MEDIAN:
{
return AggregateDataMedian.getResultFromIndex(session, on, dataType);
}
default:
DbException.throwInternalError("type=" + type);
}
}
HashMap<Expression, Object> group = select.getCurrentGroup();
if (group == null) {
throw DbException.get(ErrorCode.INVALID_USE_OF_AGGREGATE_FUNCTION_1, getSQL());
}
AggregateData data = (AggregateData) group.get(this);
if (data == null) {
data = AggregateData.create(type);
}
if (type == AggregateType.GROUP_CONCAT) {
Value[] array = ((AggregateDataCollecting) data).getArray();
if (array == null) {
return ValueNull.INSTANCE;
}
if (orderByList != null || distinct) {
sortWithOrderBy(array);
}
StatementBuilder buff = new StatementBuilder();
String sep = groupConcatSeparator == null ? "," : groupConcatSeparator.getValue(session).getString();
for (Value val : array) {
String s;
if (val.getType() == Value.ARRAY) {
s = ((ValueArray) val).getList()[0].getString();
} else {
s = val.getString();
}
if (s == null) {
continue;
}
if (sep != null) {
buff.appendExceptFirst(sep);
}
buff.append(s);
}
return ValueString.get(buff.toString());
} else if (type == AggregateType.ARRAY_AGG) {
Value[] array = ((AggregateDataCollecting) data).getArray();
if (array == null) {
return ValueNull.INSTANCE;
}
if (orderByList != null || distinct) {
sortWithOrderBy(array);
}
if (orderByList != null) {
for (int i = 0; i < array.length; i++) {
array[i] = ((ValueArray) array[i]).getList()[0];
}
}
return ValueArray.get(array);
}
return data.getValue(session.getDatabase(), dataType, distinct);
}
use of org.h2.mvstore.Cursor in project h2database by h2database.
the class LinkedIndex method find.
@Override
public Cursor find(Session session, SearchRow first, SearchRow last) {
ArrayList<Value> params = New.arrayList();
StatementBuilder buff = new StatementBuilder("SELECT * FROM ");
buff.append(targetTableName).append(" T");
for (int i = 0; first != null && i < first.getColumnCount(); i++) {
Value v = first.getValue(i);
if (v != null) {
buff.appendOnlyFirst(" WHERE ");
buff.appendExceptFirst(" AND ");
Column col = table.getColumn(i);
buff.append(col.getSQL());
if (v == ValueNull.INSTANCE) {
buff.append(" IS NULL");
} else {
buff.append(">=");
addParameter(buff, col);
params.add(v);
}
}
}
for (int i = 0; last != null && i < last.getColumnCount(); i++) {
Value v = last.getValue(i);
if (v != null) {
buff.appendOnlyFirst(" WHERE ");
buff.appendExceptFirst(" AND ");
Column col = table.getColumn(i);
buff.append(col.getSQL());
if (v == ValueNull.INSTANCE) {
buff.append(" IS NULL");
} else {
buff.append("<=");
addParameter(buff, col);
params.add(v);
}
}
}
String sql = buff.toString();
try {
PreparedStatement prep = link.execute(sql, params, false);
ResultSet rs = prep.getResultSet();
return new LinkedCursor(link, rs, session, sql, prep);
} catch (Exception e) {
throw TableLink.wrapException(sql, e);
}
}
use of org.h2.mvstore.Cursor in project h2database by h2database.
the class NonUniqueHashIndex method find.
@Override
public Cursor find(Session session, SearchRow first, SearchRow last) {
if (first == null || last == null) {
throw DbException.throwInternalError(first + " " + last);
}
if (first != last) {
if (compareKeys(first, last) != 0) {
throw DbException.throwInternalError();
}
}
Value v = first.getValue(indexColumn);
/*
* Sometimes the incoming search is a similar, but not the same type
* e.g. the search value is INT, but the index column is LONG. In which
* case we need to convert, otherwise the ValueHashMap will not find the
* result.
*/
v = v.convertTo(tableData.getColumn(indexColumn).getType());
ArrayList<Long> positions = rows.get(v);
return new NonUniqueHashCursor(session, tableData, positions);
}
Aggregations