Search in sources :

Example 61 with Column

use of org.h2.table.Column in project h2database by h2database.

the class HashIndex method getCost.

@Override
public double getCost(Session session, int[] masks, TableFilter[] filters, int filter, SortOrder sortOrder, HashSet<Column> allColumnsSet) {
    for (Column column : columns) {
        int index = column.getColumnId();
        int mask = masks[index];
        if ((mask & IndexCondition.EQUALITY) != IndexCondition.EQUALITY) {
            return Long.MAX_VALUE;
        }
    }
    return 2;
}
Also used : Column(org.h2.table.Column) IndexColumn(org.h2.table.IndexColumn)

Example 62 with Column

use of org.h2.table.Column in project h2database by h2database.

the class HashIndex method find.

@Override
public Cursor find(Session session, SearchRow first, SearchRow last) {
    if (first == null || last == null) {
        // TODO hash index: should additionally check if values are the same
        throw DbException.throwInternalError(first + " " + last);
    }
    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());
    Row result;
    Long pos = rows.get(v);
    if (pos == null) {
        result = null;
    } else {
        result = tableData.getRow(session, pos.intValue());
    }
    return new SingleRowCursor(result);
}
Also used : Value(org.h2.value.Value) Row(org.h2.result.Row) SearchRow(org.h2.result.SearchRow)

Example 63 with Column

use of org.h2.table.Column 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();
    }
}
Also used : Column(org.h2.table.Column) IndexColumn(org.h2.table.IndexColumn) Value(org.h2.value.Value) IndexColumn(org.h2.table.IndexColumn)

Example 64 with Column

use of org.h2.table.Column in project h2database by h2database.

the class PageBtreeIndex method getSearchRow.

/**
 * Create a search row for this row.
 *
 * @param row the row
 * @return the search row
 */
private SearchRow getSearchRow(Row row) {
    SearchRow r = table.getTemplateSimpleRow(columns.length == 1);
    r.setKeyAndVersion(row);
    for (Column c : columns) {
        int idx = c.getColumnId();
        r.setValue(idx, row.getValue(idx));
    }
    return r;
}
Also used : Column(org.h2.table.Column) IndexColumn(org.h2.table.IndexColumn) SearchRow(org.h2.result.SearchRow)

Example 65 with Column

use of org.h2.table.Column in project h2database by h2database.

the class PageBtreeIndex method writeRow.

/**
 * Write a row to the data page at the given offset.
 *
 * @param data the data
 * @param offset the offset
 * @param onlyPosition whether only the position of the row is stored
 * @param row the row to write
 */
void writeRow(Data data, int offset, SearchRow row, boolean onlyPosition) {
    data.setPos(offset);
    data.writeVarLong(row.getKey());
    if (!onlyPosition) {
        for (Column col : columns) {
            int idx = col.getColumnId();
            data.writeValue(row.getValue(idx));
        }
    }
}
Also used : Column(org.h2.table.Column) IndexColumn(org.h2.table.IndexColumn)

Aggregations

Column (org.h2.table.Column)146 Value (org.h2.value.Value)83 IndexColumn (org.h2.table.IndexColumn)79 DbException (org.h2.message.DbException)64 SQLException (java.sql.SQLException)60 Expression (org.h2.expression.Expression)55 ExpressionColumn (org.h2.expression.ExpressionColumn)51 ValueString (org.h2.value.ValueString)34 AlterTableAlterColumn (org.h2.command.ddl.AlterTableAlterColumn)32 ValueExpression (org.h2.expression.ValueExpression)30 ArrayList (java.util.ArrayList)27 PreparedStatement (java.sql.PreparedStatement)26 Index (org.h2.index.Index)26 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)25 Table (org.h2.table.Table)25 AlterTableRenameColumn (org.h2.command.ddl.AlterTableRenameColumn)22 GridH2Table (org.apache.ignite.internal.processors.query.h2.opt.GridH2Table)21 AlterTableAddConstraint (org.h2.command.ddl.AlterTableAddConstraint)21 StatementBuilder (org.h2.util.StatementBuilder)19 Constraint (org.h2.constraint.Constraint)18