Search in sources :

Example 1 with SqlIndexColumn

use of org.apache.ignite.internal.sql.command.SqlIndexColumn 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 2 with SqlIndexColumn

use of org.apache.ignite.internal.sql.command.SqlIndexColumn in project ignite by apache.

the class DdlStatementsProcessor method runDdlStatement.

/**
 * Run DDL statement.
 *
 * @param sql Original SQL.
 * @param cmd Command.
 * @return Result.
 * @throws IgniteCheckedException On error.
 */
@SuppressWarnings("unchecked")
public FieldsQueryCursor<List<?>> runDdlStatement(String sql, SqlCommand cmd) throws IgniteCheckedException {
    IgniteInternalFuture fut = null;
    try {
        if (cmd instanceof SqlCreateIndexCommand) {
            SqlCreateIndexCommand cmd0 = (SqlCreateIndexCommand) cmd;
            GridH2Table tbl = idx.dataTable(cmd0.schemaName(), cmd0.tableName());
            if (tbl == null)
                throw new SchemaOperationException(SchemaOperationException.CODE_TABLE_NOT_FOUND, cmd0.tableName());
            assert tbl.rowDescriptor() != null;
            isDdlSupported(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 = idx.dataTableForIndex(cmd0.schemaName(), cmd0.indexName());
            if (tbl != null) {
                isDdlSupported(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 = idx.dataTable(cmd0.schemaName(), cmd0.tableName());
            if (tbl == null) {
                ctx.cache().createMissingQueryCaches();
                tbl = idx.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.authentication().addUser(addCmd.userName(), addCmd.password());
        } else if (cmd instanceof SqlAlterUserCommand) {
            SqlAlterUserCommand altCmd = (SqlAlterUserCommand) cmd;
            ctx.authentication().updateUser(altCmd.userName(), altCmd.password());
        } else if (cmd instanceof SqlDropUserCommand) {
            SqlDropUserCommand dropCmd = (SqlDropUserCommand) cmd;
            ctx.authentication().removeUser(dropCmd.userName());
        } else
            throw new IgniteSQLException("Unsupported DDL operation: " + sql, IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
        if (fut != null)
            fut.get();
        return H2Utils.zeroCursor();
    } catch (SchemaOperationException e) {
        throw convert(e);
    } catch (IgniteSQLException e) {
        throw e;
    } catch (Exception e) {
        throw new IgniteSQLException(e.getMessage(), e);
    }
}
Also used : SchemaOperationException(org.apache.ignite.internal.processors.query.schema.SchemaOperationException) SqlCreateIndexCommand(org.apache.ignite.internal.sql.command.SqlCreateIndexCommand) SqlAlterTableCommand(org.apache.ignite.internal.sql.command.SqlAlterTableCommand) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) SqlIndexColumn(org.apache.ignite.internal.sql.command.SqlIndexColumn) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) SchemaOperationException(org.apache.ignite.internal.processors.query.schema.SchemaOperationException) LinkedHashMap(java.util.LinkedHashMap) GridQueryTypeDescriptor(org.apache.ignite.internal.processors.query.GridQueryTypeDescriptor) GridFinishedFuture(org.apache.ignite.internal.util.future.GridFinishedFuture) GridQueryProperty(org.apache.ignite.internal.processors.query.GridQueryProperty) SqlDropIndexCommand(org.apache.ignite.internal.sql.command.SqlDropIndexCommand) SqlCreateUserCommand(org.apache.ignite.internal.sql.command.SqlCreateUserCommand) SqlDropUserCommand(org.apache.ignite.internal.sql.command.SqlDropUserCommand) GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table) IgniteCluster(org.apache.ignite.IgniteCluster) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) QueryIndex(org.apache.ignite.cache.QueryIndex) SqlAlterUserCommand(org.apache.ignite.internal.sql.command.SqlAlterUserCommand)

Example 3 with SqlIndexColumn

use of org.apache.ignite.internal.sql.command.SqlIndexColumn in project ignite by apache.

the class SqlParserCreateIndexSelfTest method validate.

/**
 * Validate create index command.
 *
 * @param cmd Command.
 * @param expSchemaName Expected schema name.
 * @param expTblName Expected table name.
 * @param expIdxName Expected index name.
 * @param props Expected properties.
 * @param expColDefs Expected column definitions.
 */
private static void validate(SqlCreateIndexCommand cmd, String expSchemaName, String expTblName, String expIdxName, Map<String, Object> props, Object... expColDefs) {
    assertEquals(expSchemaName, cmd.schemaName());
    assertEquals(expTblName, cmd.tableName());
    assertEquals(expIdxName, cmd.indexName());
    Map<String, Object> cmpProps = getProps(cmd.parallel(), cmd.inlineSize());
    assertEquals(cmpProps, props);
    if (F.isEmpty(expColDefs) || expColDefs.length % 2 == 1)
        throw new IllegalArgumentException("Column definitions must be even.");
    Collection<SqlIndexColumn> cols = cmd.columns();
    assertEquals(expColDefs.length / 2, cols.size());
    Iterator<SqlIndexColumn> colIter = cols.iterator();
    for (int i = 0; i < expColDefs.length; ) {
        SqlIndexColumn col = colIter.next();
        String expColName = (String) expColDefs[i++];
        Boolean expDesc = (Boolean) expColDefs[i++];
        assertEquals(expColName, col.name());
        assertEquals(expDesc, (Boolean) col.descending());
    }
}
Also used : SqlIndexColumn(org.apache.ignite.internal.sql.command.SqlIndexColumn)

Aggregations

SqlIndexColumn (org.apache.ignite.internal.sql.command.SqlIndexColumn)3 LinkedHashMap (java.util.LinkedHashMap)2 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)2 IgniteCluster (org.apache.ignite.IgniteCluster)2 QueryIndex (org.apache.ignite.cache.QueryIndex)2 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)2 GridQueryProperty (org.apache.ignite.internal.processors.query.GridQueryProperty)2 GridQueryTypeDescriptor (org.apache.ignite.internal.processors.query.GridQueryTypeDescriptor)2 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)2 GridH2Table (org.apache.ignite.internal.processors.query.h2.opt.GridH2Table)2 SchemaOperationException (org.apache.ignite.internal.processors.query.schema.SchemaOperationException)2 SqlAlterTableCommand (org.apache.ignite.internal.sql.command.SqlAlterTableCommand)2 SqlAlterUserCommand (org.apache.ignite.internal.sql.command.SqlAlterUserCommand)2 SqlCreateIndexCommand (org.apache.ignite.internal.sql.command.SqlCreateIndexCommand)2 SqlCreateUserCommand (org.apache.ignite.internal.sql.command.SqlCreateUserCommand)2 SqlDropIndexCommand (org.apache.ignite.internal.sql.command.SqlDropIndexCommand)2 SqlDropUserCommand (org.apache.ignite.internal.sql.command.SqlDropUserCommand)2 GridFinishedFuture (org.apache.ignite.internal.util.future.GridFinishedFuture)2 SqlAnalyzeCommand (org.apache.ignite.internal.sql.command.SqlAnalyzeCommand)1 SqlDropStatisticsCommand (org.apache.ignite.internal.sql.command.SqlDropStatisticsCommand)1