Search in sources :

Example 36 with GridH2Table

use of org.apache.ignite.internal.processors.query.h2.opt.GridH2Table in project ignite by apache.

the class CommandProcessor method runCommandNativeDdl.

/**
 * Run DDL statement.
 *
 * @param sql Original SQL.
 * @param cmd Command.
 */
private void runCommandNativeDdl(String sql, SqlCommand cmd) {
    IgniteInternalFuture fut = null;
    try {
        isDdlOnSchemaSupported(cmd.schemaName());
        finishActiveTxIfNecessary();
        if (cmd instanceof SqlCreateIndexCommand) {
            SqlCreateIndexCommand cmd0 = (SqlCreateIndexCommand) cmd;
            GridH2Table tbl = schemaMgr.dataTable(cmd0.schemaName(), cmd0.tableName());
            if (tbl == null)
                throw new SchemaOperationException(SchemaOperationException.CODE_TABLE_NOT_FOUND, cmd0.tableName());
            assert tbl.rowDescriptor() != null;
            ensureDdlSupported(tbl);
            QueryIndex newIdx = new QueryIndex();
            newIdx.setName(cmd0.indexName());
            newIdx.setIndexType(cmd0.spatial() ? QueryIndexType.GEOSPATIAL : QueryIndexType.SORTED);
            LinkedHashMap<String, Boolean> flds = new LinkedHashMap<>();
            // Let's replace H2's table and property names by those operated by GridQueryProcessor.
            GridQueryTypeDescriptor typeDesc = tbl.rowDescriptor().type();
            for (SqlIndexColumn col : cmd0.columns()) {
                GridQueryProperty prop = typeDesc.property(col.name());
                if (prop == null)
                    throw new SchemaOperationException(SchemaOperationException.CODE_COLUMN_NOT_FOUND, col.name());
                flds.put(prop.name(), !col.descending());
            }
            newIdx.setFields(flds);
            newIdx.setInlineSize(cmd0.inlineSize());
            fut = ctx.query().dynamicIndexCreate(tbl.cacheName(), cmd.schemaName(), typeDesc.tableName(), newIdx, cmd0.ifNotExists(), cmd0.parallel());
        } else if (cmd instanceof SqlDropIndexCommand) {
            SqlDropIndexCommand cmd0 = (SqlDropIndexCommand) cmd;
            GridH2Table tbl = schemaMgr.dataTableForIndex(cmd0.schemaName(), cmd0.indexName());
            if (tbl != null) {
                ensureDdlSupported(tbl);
                fut = ctx.query().dynamicIndexDrop(tbl.cacheName(), cmd0.schemaName(), cmd0.indexName(), cmd0.ifExists());
            } else {
                if (cmd0.ifExists())
                    fut = new GridFinishedFuture();
                else
                    throw new SchemaOperationException(SchemaOperationException.CODE_INDEX_NOT_FOUND, cmd0.indexName());
            }
        } else if (cmd instanceof SqlAlterTableCommand) {
            SqlAlterTableCommand cmd0 = (SqlAlterTableCommand) cmd;
            GridH2Table tbl = schemaMgr.dataTable(cmd0.schemaName(), cmd0.tableName());
            if (tbl == null) {
                throw new SchemaOperationException(SchemaOperationException.CODE_TABLE_NOT_FOUND, cmd0.tableName());
            }
            Boolean logging = cmd0.logging();
            assert logging != null : "Only LOGGING/NOLOGGING are supported at the moment.";
            IgniteCluster cluster = ctx.grid().cluster();
            if (logging) {
                boolean res = cluster.enableWal(tbl.cacheName());
                if (!res)
                    throw new IgniteSQLException("Logging already enabled for table: " + cmd0.tableName());
            } else {
                boolean res = cluster.disableWal(tbl.cacheName());
                if (!res)
                    throw new IgniteSQLException("Logging already disabled for table: " + cmd0.tableName());
            }
            fut = new GridFinishedFuture();
        } else if (cmd instanceof SqlCreateUserCommand) {
            SqlCreateUserCommand addCmd = (SqlCreateUserCommand) cmd;
            ctx.security().createUser(addCmd.userName(), addCmd.password().toCharArray());
        } else if (cmd instanceof SqlAlterUserCommand) {
            SqlAlterUserCommand altCmd = (SqlAlterUserCommand) cmd;
            ctx.security().alterUser(altCmd.userName(), altCmd.password().toCharArray());
        } else if (cmd instanceof SqlDropUserCommand) {
            SqlDropUserCommand dropCmd = (SqlDropUserCommand) cmd;
            ctx.security().dropUser(dropCmd.userName());
        } else if (cmd instanceof SqlAnalyzeCommand)
            processAnalyzeCommand((SqlAnalyzeCommand) cmd);
        else if (cmd instanceof SqlRefreshStatitsicsCommand)
            processRefreshStatisticsCommand((SqlRefreshStatitsicsCommand) cmd);
        else if (cmd instanceof SqlDropStatisticsCommand)
            processDropStatisticsCommand((SqlDropStatisticsCommand) cmd);
        else
            throw new IgniteSQLException("Unsupported DDL operation: " + sql, IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
        if (fut != null)
            fut.get();
    } catch (SchemaOperationException e) {
        throw convert(e);
    } catch (IgniteSQLException e) {
        throw e;
    } catch (Exception e) {
        throw new IgniteSQLException(e.getMessage(), e);
    }
}
Also used : SqlAlterTableCommand(org.apache.ignite.internal.sql.command.SqlAlterTableCommand) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) LinkedHashMap(java.util.LinkedHashMap) GridQueryTypeDescriptor(org.apache.ignite.internal.processors.query.GridQueryTypeDescriptor) GridFinishedFuture(org.apache.ignite.internal.util.future.GridFinishedFuture) SqlCreateUserCommand(org.apache.ignite.internal.sql.command.SqlCreateUserCommand) GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table) QueryIndex(org.apache.ignite.cache.QueryIndex) SqlAnalyzeCommand(org.apache.ignite.internal.sql.command.SqlAnalyzeCommand) SqlAlterUserCommand(org.apache.ignite.internal.sql.command.SqlAlterUserCommand) SchemaOperationException(org.apache.ignite.internal.processors.query.schema.SchemaOperationException) SqlCreateIndexCommand(org.apache.ignite.internal.sql.command.SqlCreateIndexCommand) SqlDropStatisticsCommand(org.apache.ignite.internal.sql.command.SqlDropStatisticsCommand) SqlIndexColumn(org.apache.ignite.internal.sql.command.SqlIndexColumn) SqlRefreshStatitsicsCommand(org.apache.ignite.internal.sql.command.SqlRefreshStatitsicsCommand) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) SchemaOperationException(org.apache.ignite.internal.processors.query.schema.SchemaOperationException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) GridQueryProperty(org.apache.ignite.internal.processors.query.GridQueryProperty) SqlDropIndexCommand(org.apache.ignite.internal.sql.command.SqlDropIndexCommand) SqlDropUserCommand(org.apache.ignite.internal.sql.command.SqlDropUserCommand) IgniteCluster(org.apache.ignite.IgniteCluster) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException)

Example 37 with GridH2Table

use of org.apache.ignite.internal.processors.query.h2.opt.GridH2Table in project ignite by apache.

the class H2TableDescriptor method createSystemIndexes.

/**
 * Create list of indexes. First must be primary key, after that all unique indexes and only then non-unique
 * indexes. All indexes must be subtypes of {@link H2TreeIndexBase}.
 *
 * @param tbl Table to create indexes for.
 * @return List of indexes.
 */
public ArrayList<Index> createSystemIndexes(GridH2Table tbl) {
    ArrayList<Index> idxs = new ArrayList<>();
    IndexColumn keyCol = tbl.indexColumn(QueryUtils.KEY_COL, SortOrder.ASCENDING);
    IndexColumn affCol = tbl.getAffinityKeyColumn();
    if (affCol != null && H2Utils.equals(affCol, keyCol))
        affCol = null;
    List<IndexColumn> unwrappedKeyAndAffinityCols = extractKeyColumns(tbl, keyCol, affCol);
    List<IndexColumn> wrappedKeyCols = H2Utils.treeIndexColumns(tbl.rowDescriptor(), new ArrayList<>(2), keyCol, affCol);
    Index hashIdx = createHashIndex(tbl, wrappedKeyCols);
    if (hashIdx != null)
        idxs.add(hashIdx);
    // Add primary key index.
    Index pkIdx = idx.createSortedIndex(PK_IDX_NAME, tbl, true, false, unwrappedKeyAndAffinityCols, wrappedKeyCols, tbl.rowDescriptor().tableDescriptor().type().primaryKeyInlineSize(), null);
    idxs.add(pkIdx);
    if (type().valueClass() == String.class && !idx.distributedConfiguration().isDisableCreateLuceneIndexForStringValueType()) {
        try {
            luceneIdx = new GridLuceneIndex(idx.kernalContext(), tbl.cacheName(), type);
        } catch (IgniteCheckedException e1) {
            throw new IgniteException(e1);
        }
    }
    GridQueryIndexDescriptor textIdx = type.textIndex();
    if (textIdx != null) {
        try {
            luceneIdx = new GridLuceneIndex(idx.kernalContext(), tbl.cacheName(), type);
        } catch (IgniteCheckedException e1) {
            throw new IgniteException(e1);
        }
    }
    // Locate index where affinity column is first (if any).
    if (affCol != null) {
        boolean affIdxFound = false;
        for (GridQueryIndexDescriptor idxDesc : type.indexes().values()) {
            if (idxDesc.type() != QueryIndexType.SORTED)
                continue;
            String firstField = idxDesc.fields().iterator().next();
            Column col = tbl.getColumn(firstField);
            IndexColumn idxCol = tbl.indexColumn(col.getColumnId(), idxDesc.descending(firstField) ? SortOrder.DESCENDING : SortOrder.ASCENDING);
            affIdxFound |= H2Utils.equals(idxCol, affCol);
        }
        // Add explicit affinity key index if nothing alike was found.
        if (!affIdxFound) {
            List<IndexColumn> unwrappedKeyCols = extractKeyColumns(tbl, keyCol, null);
            ArrayList<IndexColumn> colsWithUnwrappedKey = new ArrayList<>(unwrappedKeyCols.size());
            colsWithUnwrappedKey.add(affCol);
            // We need to reorder PK columns to have affinity key as first column, that's why we can't use simple PK columns
            H2Utils.addUniqueColumns(colsWithUnwrappedKey, unwrappedKeyCols);
            List<IndexColumn> cols = H2Utils.treeIndexColumns(tbl.rowDescriptor(), new ArrayList<>(2), affCol, keyCol);
            idxs.add(idx.createSortedIndex(AFFINITY_KEY_IDX_NAME, tbl, false, true, colsWithUnwrappedKey, cols, tbl.rowDescriptor().tableDescriptor().type().affinityFieldInlineSize(), null));
        }
    }
    return idxs;
}
Also used : ArrayList(java.util.ArrayList) GridLuceneIndex(org.apache.ignite.internal.processors.query.h2.opt.GridLuceneIndex) GridLuceneIndex(org.apache.ignite.internal.processors.query.h2.opt.GridLuceneIndex) Index(org.h2.index.Index) H2PkHashIndex(org.apache.ignite.internal.processors.query.h2.database.H2PkHashIndex) IndexColumn(org.h2.table.IndexColumn) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Column(org.h2.table.Column) IndexColumn(org.h2.table.IndexColumn) IgniteException(org.apache.ignite.IgniteException) GridQueryIndexDescriptor(org.apache.ignite.internal.processors.query.GridQueryIndexDescriptor)

Example 38 with GridH2Table

use of org.apache.ignite.internal.processors.query.h2.opt.GridH2Table in project ignite by apache.

the class BasicIndexTest method testCreateSystemIndexWithSpecifiedInlineSizeByDdl.

/**
 */
@Test
public void testCreateSystemIndexWithSpecifiedInlineSizeByDdl() throws Exception {
    inlineSize = 10;
    final int pkInlineSize = 22;
    final int affInlineSize = 23;
    IgniteEx ign = startGrid();
    sql("CREATE TABLE TEST (ID VARCHAR, ID_AFF INT, VAL INT, " + "PRIMARY KEY (ID, ID_AFF)) WITH" + "\"" + "AFFINITY_KEY=ID_AFF," + "PK_INLINE_SIZE=" + pkInlineSize + "," + "AFFINITY_INDEX_INLINE_SIZE=" + affInlineSize + "\"");
    GridH2Table tbl = ((IgniteH2Indexing) ign.context().query().getIndexing()).schemaManager().dataTable("PUBLIC", "TEST");
    assertEquals(pkInlineSize, ((H2TreeIndex) tbl.getIndex("_key_PK")).inlineSize());
    assertEquals(affInlineSize, ((H2TreeIndex) tbl.getIndex("AFFINITY_KEY")).inlineSize());
}
Also used : IgniteEx(org.apache.ignite.internal.IgniteEx) GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table) Test(org.junit.Test)

Example 39 with GridH2Table

use of org.apache.ignite.internal.processors.query.h2.opt.GridH2Table in project ignite by apache.

the class CollocationModel method calculate.

/**
 * Do the needed calculations.
 */
@SuppressWarnings("ConstantConditions")
private void calculate() {
    if (type != null)
        return;
    if (view) {
        // We are at (sub-)query model.
        assert childFilters != null;
        boolean collocated = true;
        boolean partitioned = false;
        CollocationModelMultiplier maxMultiplier = CollocationModelMultiplier.COLLOCATED;
        for (int i = 0; i < childFilters.length; i++) {
            CollocationModel child = child(i, true);
            CollocationModelType t = child.type(true);
            if (child.multiplier == CollocationModelMultiplier.REPLICATED_NOT_LAST)
                maxMultiplier = child.multiplier;
            if (t.isPartitioned()) {
                partitioned = true;
                if (!t.isCollocated()) {
                    collocated = false;
                    CollocationModelMultiplier m = child.multiplier(true);
                    if (m.multiplier() > maxMultiplier.multiplier()) {
                        maxMultiplier = m;
                        if (maxMultiplier == CollocationModelMultiplier.REPLICATED_NOT_LAST)
                            break;
                    }
                }
            }
        }
        type = CollocationModelType.of(partitioned, collocated);
        multiplier = maxMultiplier;
    } else {
        assert upper != null;
        assert childFilters == null;
        // We are at table instance.
        Table tbl = filter().getTable();
        // Only partitioned tables will do distributed joins.
        if (!(tbl instanceof GridH2Table) || !((GridH2Table) tbl).isPartitioned()) {
            type = CollocationModelType.REPLICATED;
            multiplier = CollocationModelMultiplier.COLLOCATED;
            return;
        }
        // to all the affinity nodes the "base" does not need to get remote results.
        if (!upper.isPartitionedTableBeforeExists(filter)) {
            type = CollocationModelType.PARTITIONED_COLLOCATED;
            multiplier = CollocationModelMultiplier.COLLOCATED;
        } else {
            // collocated. If we at least have affinity key condition, then we do unicast which is cheaper.
            switch(upper.joinedWithCollocated(filter)) {
                case COLLOCATED_JOIN:
                    type = CollocationModelType.PARTITIONED_COLLOCATED;
                    multiplier = CollocationModelMultiplier.COLLOCATED;
                    break;
                case HAS_AFFINITY_CONDITION:
                    type = CollocationModelType.PARTITIONED_NOT_COLLOCATED;
                    multiplier = CollocationModelMultiplier.UNICAST;
                    break;
                case NONE:
                    type = CollocationModelType.PARTITIONED_NOT_COLLOCATED;
                    multiplier = CollocationModelMultiplier.BROADCAST;
                    break;
                default:
                    throw new IllegalStateException();
            }
        }
        if (upper.isPreviousTableReplicated(filter))
            multiplier = CollocationModelMultiplier.REPLICATED_NOT_LAST;
    }
}
Also used : GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table) Table(org.h2.table.Table) GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table)

Example 40 with GridH2Table

use of org.apache.ignite.internal.processors.query.h2.opt.GridH2Table in project ignite by apache.

the class CollocationModel method buildCollocationModel.

/**
 * @param upper Upper.
 * @param filter Filter.
 * @param qry Query.
 * @param unions Unions.
 * @param validate Query validation flag.
 * @return Built model.
 */
private static CollocationModel buildCollocationModel(CollocationModel upper, int filter, Query qry, List<CollocationModel> unions, boolean validate) {
    if (qry.isUnion()) {
        if (unions == null)
            unions = new ArrayList<>();
        SelectUnion union = (SelectUnion) qry;
        CollocationModel left = buildCollocationModel(upper, filter, union.getLeft(), unions, validate);
        CollocationModel right = buildCollocationModel(upper, filter, union.getRight(), unions, validate);
        assert left != null;
        assert right != null;
        return upper != null ? upper : left;
    }
    Select select = (Select) qry;
    List<TableFilter> list = new ArrayList<>();
    for (TableFilter f = select.getTopTableFilter(); f != null; f = f.getJoin()) list.add(f);
    TableFilter[] filters = list.toArray(EMPTY_FILTERS);
    CollocationModel cm = createChildModel(upper, filter, unions, true, validate);
    cm.childFilters(filters);
    for (int i = 0; i < filters.length; i++) {
        TableFilter f = filters[i];
        if (f.getTable().isView())
            buildCollocationModel(cm, i, getSubQuery(f), null, validate);
        else if (f.getTable() instanceof GridH2Table)
            createChildModel(cm, i, null, false, validate);
    }
    return upper != null ? upper : cm;
}
Also used : SelectUnion(org.h2.command.dml.SelectUnion) TableFilter(org.h2.table.TableFilter) GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table) ArrayList(java.util.ArrayList) Select(org.h2.command.dml.Select)

Aggregations

GridH2Table (org.apache.ignite.internal.processors.query.h2.opt.GridH2Table)66 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)26 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)26 GridH2RowDescriptor (org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor)18 GridSqlColumn (org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn)18 Column (org.h2.table.Column)18 ArrayList (java.util.ArrayList)16 IgniteException (org.apache.ignite.IgniteException)15 HashSet (java.util.HashSet)13 GridSqlElement (org.apache.ignite.internal.processors.query.h2.sql.GridSqlElement)12 GridQueryTypeDescriptor (org.apache.ignite.internal.processors.query.GridQueryTypeDescriptor)11 GridSqlTable (org.apache.ignite.internal.processors.query.h2.sql.GridSqlTable)11 SQLException (java.sql.SQLException)10 List (java.util.List)10 GridCacheContext (org.apache.ignite.internal.processors.cache.GridCacheContext)10 GridSqlSelect (org.apache.ignite.internal.processors.query.h2.sql.GridSqlSelect)8 Map (java.util.Map)7 IgniteEx (org.apache.ignite.internal.IgniteEx)7 GridQueryProperty (org.apache.ignite.internal.processors.query.GridQueryProperty)7 HashMap (java.util.HashMap)5