Search in sources :

Example 11 with Index

use of org.hsqldb_voltpatches.index.Index in project voltdb by VoltDB.

the class TableBase method createIndexStructure.

final Index createIndexStructure(HsqlName name, int[] columns, boolean[] descending, boolean[] nullsLast, boolean unique, boolean constraint, boolean forward) {
    if (primaryKeyCols == null) {
        // A VoltDB extension to support matview-based indexes
        primaryKeyCols = new int[0];
    /* disable 1 line ...
            throw Error.runtimeError(ErrorCode.U_S0500, "createIndex");
            ... disabled 1 line */
    // End of VoltDB extension
    }
    int s = columns.length;
    int[] cols = new int[s];
    Type[] types = new Type[s];
    for (int j = 0; j < s; j++) {
        cols[j] = columns[j];
        types[j] = colTypes[cols[j]];
    }
    long id = database.persistentStoreCollection.getNextId();
    Index newIndex = new IndexAVL(name, id, this, cols, descending, nullsLast, types, false, unique, constraint, forward);
    return newIndex;
}
Also used : Type(org.hsqldb_voltpatches.types.Type) Index(org.hsqldb_voltpatches.index.Index) IndexAVL(org.hsqldb_voltpatches.index.IndexAVL)

Example 12 with Index

use of org.hsqldb_voltpatches.index.Index in project voltdb by VoltDB.

the class TableBase method createPrimaryIndex.

public final void createPrimaryIndex(int[] pkcols, Type[] pktypes, HsqlName name) {
    long id = database.persistentStoreCollection.getNextId();
    Index newindex = new IndexAVL(name, id, this, pkcols, null, null, pktypes, true, true, true, false);
    try {
        addIndex(newindex);
    } catch (HsqlException e) {
    }
}
Also used : Index(org.hsqldb_voltpatches.index.Index) IndexAVL(org.hsqldb_voltpatches.index.IndexAVL)

Example 13 with Index

use of org.hsqldb_voltpatches.index.Index in project voltdb by VoltDB.

the class RangeVariableResolver method assignToRangeVariable.

/**
     * Assigns a set of conditions to a range variable.
     */
void assignToRangeVariable(RangeVariable rangeVar, int rangeVarIndex, HsqlArrayList exprList, boolean isJoin) {
    if (exprList.isEmpty()) {
        return;
    }
    colIndexSetEqual.clear();
    colIndexSetOther.clear();
    for (int j = 0, size = exprList.size(); j < size; j++) {
        Expression e = (Expression) exprList.get(j);
        if (rangeVar.hasIndexCondition()) {
            rangeVar.addCondition(e, isJoin);
            exprList.set(j, null);
            continue;
        }
        if (e.getIndexableExpression(rangeVar) == null) {
            rangeVar.addCondition(e, isJoin);
            exprList.set(j, null);
            continue;
        }
        // can use index
        int type = e.getType();
        switch(type) {
            default:
                {
                    int colIndex = e.getLeftNode().getColumnIndex();
                    colIndexSetOther.add(colIndex);
                    break;
                }
            case OpTypes.EQUAL:
                if (e.exprSubType == OpTypes.ANY_QUANTIFIED) {
                    Index index = rangeVar.rangeTable.getIndexForColumn(e.getLeftNode().nodes[0].getColumnIndex());
                    //                        index = null;
                    if (index != null && inExpressions[rangeVarIndex] == null) {
                        inExpressions[rangeVarIndex] = e;
                        inExpressionCount++;
                    } else {
                        rangeVar.addCondition(e, isJoin);
                    }
                    exprList.set(j, null);
                    continue;
                }
            // $FALL-THROUGH$
            case OpTypes.IS_NULL:
                {
                    int colIndex = e.getLeftNode().getColumnIndex();
                    colIndexSetEqual.add(colIndex);
                    break;
                }
            case OpTypes.NOT:
                {
                    int colIndex = e.getLeftNode().getLeftNode().getColumnIndex();
                    colIndexSetOther.add(colIndex);
                    break;
                }
        }
    }
    boolean isEqual = true;
    Index idx = rangeVar.rangeTable.getIndexForColumns(colIndexSetEqual);
    if (idx == null) {
        isEqual = false;
        idx = rangeVar.rangeTable.getIndexForColumns(colIndexSetOther);
    }
    // different procedure for subquery tables
    if (idx == null && rangeVar.rangeTable.isSessionBased) {
        if (!colIndexSetEqual.isEmpty()) {
            int[] cols = colIndexSetEqual.toArray();
            idx = rangeVar.rangeTable.getIndexForColumns(cols);
        }
        if (idx == null && !colIndexSetOther.isEmpty()) {
            int[] cols = colIndexSetOther.toArray();
            idx = rangeVar.rangeTable.getIndexForColumns(cols);
        }
    }
    // no index found
    if (idx == null) {
        for (int j = 0, size = exprList.size(); j < size; j++) {
            Expression e = (Expression) exprList.get(j);
            if (e != null) {
                rangeVar.addCondition(e, isJoin);
            }
        }
        return;
    }
    // index found
    int[] cols = idx.getColumns();
    int colCount = cols.length;
    if (isEqual && colCount > 1) {
        Expression[] firstRowExpressions = new Expression[cols.length];
        for (int j = 0; j < exprList.size(); j++) {
            Expression e = (Expression) exprList.get(j);
            if (e == null) {
                continue;
            }
            int type = e.getType();
            if (type == OpTypes.EQUAL) {
                int offset = ArrayUtil.find(cols, e.getLeftNode().getColumnIndex());
                if (offset != -1 && firstRowExpressions[offset] == null) {
                    firstRowExpressions[offset] = e;
                    exprList.set(j, null);
                    continue;
                }
            }
            // not used in index lookup
            rangeVar.addCondition(e, isJoin);
            exprList.set(j, null);
        }
        boolean hasNull = false;
        for (int i = 0; i < firstRowExpressions.length; i++) {
            Expression e = firstRowExpressions[i];
            if (e == null) {
                if (colCount == cols.length) {
                    colCount = i;
                }
                hasNull = true;
                continue;
            }
            if (hasNull) {
                rangeVar.addCondition(e, isJoin);
                firstRowExpressions[i] = null;
            }
        }
        rangeVar.addIndexCondition(firstRowExpressions, idx, colCount, isJoin);
        return;
    }
    for (int j = 0; j < exprList.size(); j++) {
        Expression e = (Expression) exprList.get(j);
        if (e == null) {
            continue;
        }
        if (rangeVar.hasIndexCondition()) {
            rangeVar.addCondition(e, isJoin);
            exprList.set(j, null);
            continue;
        }
        boolean isIndexed = false;
        if (e.getType() == OpTypes.NOT && cols[0] == e.getLeftNode().getLeftNode().getColumnIndex()) {
            isIndexed = true;
        }
        if (cols[0] == e.getLeftNode().getColumnIndex()) {
            if (e.getRightNode() != null && !e.getRightNode().isCorrelated()) {
                isIndexed = true;
            }
            if (e.getType() == OpTypes.IS_NULL) {
                isIndexed = true;
            }
        }
        if (isIndexed) {
            rangeVar.addIndexCondition(e, idx, isJoin);
        } else {
            rangeVar.addCondition(e, isJoin);
        }
        exprList.set(j, null);
    }
}
Also used : Index(org.hsqldb_voltpatches.index.Index)

Example 14 with Index

use of org.hsqldb_voltpatches.index.Index in project voltdb by VoltDB.

the class Constraint method checkReferencedRows.

/**
     * Check used before creating a new foreign key cosntraint, this method
     * checks all rows of a table to ensure they all have a corresponding
     * row in the main table.
     */
void checkReferencedRows(Session session, Table table, int[] rowColArray) {
    Index mainIndex = getMainIndex();
    PersistentStore store = session.sessionData.getRowStore(table);
    RowIterator it = table.rowIterator(session);
    while (true) {
        Row row = it.getNextRow();
        if (row == null) {
            break;
        }
        Object[] rowData = row.getData();
        if (ArrayUtil.hasNull(rowData, rowColArray)) {
            if (core.matchType == OpTypes.MATCH_SIMPLE) {
                continue;
            }
        } else if (mainIndex.exists(session, store, rowData, rowColArray)) {
            continue;
        }
        if (ArrayUtil.hasAllNull(rowData, rowColArray)) {
            continue;
        }
        String colValues = "";
        for (int i = 0; i < rowColArray.length; i++) {
            Object o = rowData[rowColArray[i]];
            colValues += table.getColumnTypes()[i].convertToString(o);
            colValues += ",";
        }
        String[] info = new String[] { getName().name, getMain().getName().name };
        throw Error.error(ErrorCode.X_23502, ErrorCode.CONSTRAINT, info);
    }
}
Also used : RowIterator(org.hsqldb_voltpatches.navigator.RowIterator) PersistentStore(org.hsqldb_voltpatches.persist.PersistentStore) Index(org.hsqldb_voltpatches.index.Index)

Example 15 with Index

use of org.hsqldb_voltpatches.index.Index in project voltdb by VoltDB.

the class TableWorks method addColumn.

void addColumn(ColumnSchema column, int colIndex, HsqlArrayList constraints) {
    Index index = null;
    Table originalTable = table;
    Constraint mainConstraint = null;
    boolean addFK = false;
    boolean addUnique = false;
    boolean addCheck = false;
    checkAddColumn(column);
    Constraint c = (Constraint) constraints.get(0);
    if (c.getConstraintType() == Constraint.PRIMARY_KEY) {
        c.core.mainCols = new int[] { colIndex };
        database.schemaManager.checkSchemaObjectNotExists(c.getName());
        if (table.hasPrimaryKey()) {
            throw Error.error(ErrorCode.X_42530);
        }
        addUnique = true;
    } else {
        c = null;
    }
    table = table.moveDefinition(session, table.tableType, column, c, null, colIndex, 1, emptySet, emptySet);
    for (int i = 1; i < constraints.size(); i++) {
        c = (Constraint) constraints.get(i);
        switch(c.constType) {
            case Constraint.UNIQUE:
                {
                    if (addUnique) {
                        throw Error.error(ErrorCode.X_42522);
                    }
                    addUnique = true;
                    c.core.mainCols = new int[] { colIndex };
                    database.schemaManager.checkSchemaObjectNotExists(c.getName());
                    HsqlName indexName = database.nameManager.newAutoName("IDX", c.getName().name, table.getSchemaName(), table.getName(), SchemaObject.INDEX);
                    // create an autonamed index
                    index = table.createAndAddIndexStructure(indexName, c.getMainColumns(), null, null, true, true, false);
                    // A VoltDB extension to support the assume unique attribute
                    index = index.setAssumeUnique(c.assumeUnique);
                    // End of VoltDB extension
                    c.core.mainTable = table;
                    c.core.mainIndex = index;
                    table.addConstraint(c);
                    break;
                }
            case Constraint.FOREIGN_KEY:
                {
                    if (addFK) {
                        throw Error.error(ErrorCode.X_42528);
                    }
                    addFK = true;
                    c.core.refCols = new int[] { colIndex };
                    boolean isSelf = originalTable == c.core.mainTable;
                    if (isSelf) {
                        c.core.mainTable = table;
                    }
                    c.setColumnsIndexes(table);
                    checkCreateForeignKey(c);
                    Constraint uniqueConstraint = c.core.mainTable.getUniqueConstraintForColumns(c.core.mainCols, c.core.refCols);
                    boolean isForward = c.core.mainTable.getSchemaName() != table.getSchemaName();
                    int offset = database.schemaManager.getTableIndex(originalTable);
                    if (!isSelf && offset < database.schemaManager.getTableIndex(c.core.mainTable)) {
                        isForward = true;
                    }
                    HsqlName indexName = database.nameManager.newAutoName("IDX", c.getName().name, table.getSchemaName(), table.getName(), SchemaObject.INDEX);
                    index = table.createAndAddIndexStructure(indexName, c.getRefColumns(), null, null, false, true, isForward);
                    c.core.uniqueName = uniqueConstraint.getName();
                    c.core.mainName = database.nameManager.newAutoName("REF", c.core.refName.name, table.getSchemaName(), table.getName(), SchemaObject.INDEX);
                    c.core.mainIndex = uniqueConstraint.getMainIndex();
                    c.core.refIndex = index;
                    c.isForward = isForward;
                    table.addConstraint(c);
                    mainConstraint = new Constraint(c.core.mainName, c);
                    break;
                }
            case Constraint.CHECK:
                if (addCheck) {
                    throw Error.error(ErrorCode.X_42528);
                }
                addCheck = true;
                c.prepareCheckConstraint(session, table, false);
                table.addConstraint(c);
                if (c.isNotNull()) {
                    column.setNullable(false);
                    table.setColumnTypeVars(colIndex);
                }
                break;
        }
    }
    table.moveData(session, originalTable, colIndex, 1);
    if (mainConstraint != null) {
        mainConstraint.getMain().addConstraint(mainConstraint);
    }
    registerConstraintNames(constraints);
    setNewTableInSchema(table);
    updateConstraints(table, emptySet);
    database.persistentStoreCollection.releaseStore(originalTable);
    database.schemaManager.recompileDependentObjects(table);
}
Also used : Index(org.hsqldb_voltpatches.index.Index) HsqlName(org.hsqldb_voltpatches.HsqlNameManager.HsqlName)

Aggregations

Index (org.hsqldb_voltpatches.index.Index)29 HsqlName (org.hsqldb_voltpatches.HsqlNameManager.HsqlName)6 RowIterator (org.hsqldb_voltpatches.navigator.RowIterator)4 PersistentStore (org.hsqldb_voltpatches.persist.PersistentStore)3 IndexAVL (org.hsqldb_voltpatches.index.IndexAVL)2 HashMappedList (org.hsqldb_voltpatches.lib.HashMappedList)2 HashMap (java.util.HashMap)1 HSQLParseException (org.hsqldb_voltpatches.HSQLInterface.HSQLParseException)1 HsqlArrayList (org.hsqldb_voltpatches.lib.HsqlArrayList)1 OrderedHashSet (org.hsqldb_voltpatches.lib.OrderedHashSet)1 Type (org.hsqldb_voltpatches.types.Type)1