Search in sources :

Example 11 with IndexColumn

use of org.h2.table.IndexColumn in project ignite by apache.

the class H2Utils method createSpatialIndex.

/**
     * Create spatial index.
     *
     * @param tbl Table.
     * @param idxName Index name.
     * @param cols Columns.
     */
public static GridH2IndexBase createSpatialIndex(GridH2Table tbl, String idxName, IndexColumn[] cols) {
    try {
        Class<?> cls = Class.forName(SPATIAL_IDX_CLS);
        Constructor<?> ctor = cls.getConstructor(GridH2Table.class, String.class, Integer.TYPE, IndexColumn[].class);
        if (!ctor.isAccessible())
            ctor.setAccessible(true);
        final int segments = tbl.rowDescriptor().context().config().getQueryParallelism();
        return (GridH2IndexBase) ctor.newInstance(tbl, idxName, segments, cols);
    } catch (Exception e) {
        throw new IgniteException("Failed to instantiate: " + SPATIAL_IDX_CLS, e);
    }
}
Also used : GridH2IndexBase(org.apache.ignite.internal.processors.query.h2.opt.GridH2IndexBase) IgniteException(org.apache.ignite.IgniteException) IgniteException(org.apache.ignite.IgniteException) SQLException(java.sql.SQLException) IndexColumn(org.h2.table.IndexColumn)

Example 12 with IndexColumn

use of org.h2.table.IndexColumn in project ignite by apache.

the class H2TableDescriptor method createUserIndex.

/**
     * Create user index.
     *
     * @param idxDesc Index descriptor.
     * @return Index.
     */
public GridH2IndexBase createUserIndex(GridQueryIndexDescriptor idxDesc) {
    IndexColumn keyCol = tbl.indexColumn(KEY_COL, SortOrder.ASCENDING);
    IndexColumn affCol = tbl.getAffinityKeyColumn();
    List<IndexColumn> cols = new ArrayList<>(idxDesc.fields().size() + 2);
    for (String field : idxDesc.fields()) {
        Column col = tbl.getColumn(field);
        cols.add(tbl.indexColumn(col.getColumnId(), idxDesc.descending(field) ? SortOrder.DESCENDING : SortOrder.ASCENDING));
    }
    GridH2RowDescriptor desc = tbl.rowDescriptor();
    if (idxDesc.type() == QueryIndexType.SORTED) {
        cols = H2Utils.treeIndexColumns(desc, cols, keyCol, affCol);
        return idx.createSortedIndex(schema, idxDesc.name(), tbl, false, cols, idxDesc.inlineSize());
    } else if (idxDesc.type() == QueryIndexType.GEOSPATIAL) {
        return H2Utils.createSpatialIndex(tbl, idxDesc.name(), cols.toArray(new IndexColumn[cols.size()]));
    }
    throw new IllegalStateException("Index type: " + idxDesc.type());
}
Also used : GridH2RowDescriptor(org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor) Column(org.h2.table.Column) IndexColumn(org.h2.table.IndexColumn) ArrayList(java.util.ArrayList) IndexColumn(org.h2.table.IndexColumn)

Example 13 with IndexColumn

use of org.h2.table.IndexColumn in project ignite by apache.

the class GridSqlQueryParser method parseCreateIndex.

/**
     * Parse {@code CREATE INDEX} statement.
     *
     * @param createIdx {@code CREATE INDEX} statement.
     * @see <a href="http://h2database.com/html/grammar.html#create_index">H2 {@code CREATE INDEX} spec.</a>
     */
private GridSqlCreateIndex parseCreateIndex(CreateIndex createIdx) {
    if (CREATE_INDEX_HASH.get(createIdx) || CREATE_INDEX_PRIMARY_KEY.get(createIdx) || CREATE_INDEX_UNIQUE.get(createIdx))
        throw new IgniteSQLException("Only SPATIAL modifier is supported for CREATE INDEX", IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
    GridSqlCreateIndex res = new GridSqlCreateIndex();
    Schema schema = SCHEMA_COMMAND_SCHEMA.get(createIdx);
    String tblName = CREATE_INDEX_TABLE_NAME.get(createIdx);
    res.schemaName(schema.getName());
    res.tableName(tblName);
    res.ifNotExists(CREATE_INDEX_IF_NOT_EXISTS.get(createIdx));
    QueryIndex idx = new QueryIndex();
    idx.setName(CREATE_INDEX_NAME.get(createIdx));
    idx.setIndexType(CREATE_INDEX_SPATIAL.get(createIdx) ? QueryIndexType.GEOSPATIAL : QueryIndexType.SORTED);
    IndexColumn[] cols = CREATE_INDEX_COLUMNS.get(createIdx);
    LinkedHashMap<String, Boolean> flds = new LinkedHashMap<>(cols.length);
    for (IndexColumn col : CREATE_INDEX_COLUMNS.get(createIdx)) {
        int sortType = INDEX_COLUMN_SORT_TYPE.get(col);
        if ((sortType & SortOrder.NULLS_FIRST) != 0 || (sortType & SortOrder.NULLS_LAST) != 0)
            throw new IgniteSQLException("NULLS FIRST and NULLS LAST modifiers are not supported for index columns", IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
        flds.put(INDEX_COLUMN_NAME.get(col), (sortType & SortOrder.DESCENDING) == 0);
    }
    idx.setFields(flds);
    res.index(idx);
    return res;
}
Also used : Schema(org.h2.schema.Schema) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) QueryIndex(org.apache.ignite.cache.QueryIndex) AlterTableAddConstraint(org.h2.command.ddl.AlterTableAddConstraint) IndexColumn(org.h2.table.IndexColumn) LinkedHashMap(java.util.LinkedHashMap)

Example 14 with IndexColumn

use of org.h2.table.IndexColumn in project ignite by apache.

the class GridH2Table method createDuplicateIndexIfNeeded.

/**
     * Creates proxy index for given target index.
     * Proxy index refers to alternative key and val columns.
     *
     * @param target Index to clone.
     * @return Proxy index.
     */
public Index createDuplicateIndexIfNeeded(Index target) {
    if (!(target instanceof H2TreeIndex) && !(target instanceof SpatialIndex))
        return null;
    IndexColumn[] cols = target.getIndexColumns();
    List<IndexColumn> proxyCols = new ArrayList<>(cols.length);
    boolean modified = false;
    for (IndexColumn col : cols) {
        IndexColumn proxyCol = new IndexColumn();
        proxyCol.columnName = col.columnName;
        proxyCol.column = col.column;
        proxyCol.sortType = col.sortType;
        int altColId = desc.getAlternativeColumnId(proxyCol.column.getColumnId());
        if (altColId != proxyCol.column.getColumnId()) {
            proxyCol.column = getColumn(altColId);
            proxyCol.columnName = proxyCol.column.getName();
            modified = true;
        }
        proxyCols.add(proxyCol);
    }
    if (modified) {
        String proxyName = target.getName() + "_proxy";
        if (target.getIndexType().isSpatial())
            return new GridH2ProxySpatialIndex(this, proxyName, proxyCols, target);
        return new GridH2ProxyIndex(this, proxyName, proxyCols, target);
    }
    return null;
}
Also used : H2TreeIndex(org.apache.ignite.internal.processors.query.h2.database.H2TreeIndex) SpatialIndex(org.h2.index.SpatialIndex) ArrayList(java.util.ArrayList) IndexColumn(org.h2.table.IndexColumn)

Example 15 with IndexColumn

use of org.h2.table.IndexColumn in project ignite by apache.

the class GridH2CollocationModel method joinedWithCollocated.

/**
     * @param f Filter.
     * @return Affinity join type.
     */
private Affinity joinedWithCollocated(int f) {
    TableFilter tf = childFilters[f];
    GridH2Table tbl = (GridH2Table) tf.getTable();
    if (validate) {
        if (tbl.rowDescriptor().context().customAffinityMapper())
            throw customAffinityError(tbl.cacheName());
        if (F.isEmpty(tf.getIndexConditions())) {
            throw new CacheException("Failed to prepare distributed join query: " + "join condition does not use index [joinedCache=" + tbl.cacheName() + ", plan=" + tf.getSelect().getPlanSQL() + ']');
        }
    }
    IndexColumn affCol = tbl.getAffinityKeyColumn();
    boolean affKeyCondFound = false;
    if (affCol != null) {
        ArrayList<IndexCondition> idxConditions = tf.getIndexConditions();
        int affColId = affCol.column.getColumnId();
        for (int i = 0; i < idxConditions.size(); i++) {
            IndexCondition c = idxConditions.get(i);
            int colId = c.getColumn().getColumnId();
            int cmpType = c.getCompareType();
            if ((cmpType == Comparison.EQUAL || cmpType == Comparison.EQUAL_NULL_SAFE) && (colId == affColId || tbl.rowDescriptor().isKeyColumn(colId)) && c.isEvaluatable()) {
                affKeyCondFound = true;
                Expression exp = c.getExpression();
                exp = exp.getNonAliasExpression();
                if (exp instanceof ExpressionColumn) {
                    ExpressionColumn expCol = (ExpressionColumn) exp;
                    // This is one of our previous joins.
                    TableFilter prevJoin = expCol.getTableFilter();
                    if (prevJoin != null) {
                        GridH2CollocationModel cm = child(indexOf(prevJoin), true);
                        // different affinity columns from different tables.
                        if (cm != null && !cm.view) {
                            Type t = cm.type(true);
                            if (t.isPartitioned() && t.isCollocated() && isAffinityColumn(prevJoin, expCol, validate))
                                return Affinity.COLLOCATED_JOIN;
                        }
                    }
                }
            }
        }
    }
    return affKeyCondFound ? Affinity.HAS_AFFINITY_CONDITION : Affinity.NONE;
}
Also used : TableFilter(org.h2.table.TableFilter) CacheException(javax.cache.CacheException) Expression(org.h2.expression.Expression) IndexCondition(org.h2.index.IndexCondition) IndexColumn(org.h2.table.IndexColumn) ExpressionColumn(org.h2.expression.ExpressionColumn)

Aggregations

IndexColumn (org.h2.table.IndexColumn)17 Column (org.h2.table.Column)5 ArrayList (java.util.ArrayList)4 GridH2RowDescriptor (org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor)3 ExpressionColumn (org.h2.expression.ExpressionColumn)3 LinkedHashMap (java.util.LinkedHashMap)2 CacheException (javax.cache.CacheException)2 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)2 IgniteException (org.apache.ignite.IgniteException)2 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)2 SB (org.apache.ignite.internal.util.typedef.internal.SB)2 AlterTableAddConstraint (org.h2.command.ddl.AlterTableAddConstraint)2 Query (org.h2.command.dml.Query)2 Schema (org.h2.schema.Schema)2 Value (org.h2.value.Value)2 SQLException (java.sql.SQLException)1 HashMap (java.util.HashMap)1 IdentityHashMap (java.util.IdentityHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 Map (java.util.Map)1