Search in sources :

Example 61 with Table

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

the class IgniteH2Indexing method dropTable.

/**
 * Drops table form h2 database and clear all related indexes (h2 text, lucene).
 *
 * @param tbl Table to unregister.
 * @throws IgniteCheckedException If failed to unregister.
 */
private void dropTable(H2TableDescriptor tbl) throws IgniteCheckedException {
    assert tbl != null;
    if (log.isDebugEnabled())
        log.debug("Removing query index table: " + tbl.fullTableName());
    Connection c = connectionForThread(tbl.schemaName());
    Statement stmt = null;
    try {
        stmt = c.createStatement();
        String sql = "DROP TABLE IF EXISTS " + tbl.fullTableName();
        if (log.isDebugEnabled())
            log.debug("Dropping database index table with SQL: " + sql);
        stmt.executeUpdate(sql);
    } catch (SQLException e) {
        onSqlException();
        throw new IgniteSQLException("Failed to drop database index table [type=" + tbl.type().name() + ", table=" + tbl.fullTableName() + "]", IgniteQueryErrorCode.TABLE_DROP_FAILED, e);
    } finally {
        U.close(stmt, log);
    }
}
Also used : SQLException(java.sql.SQLException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) PreparedStatement(java.sql.PreparedStatement) JdbcStatement(org.h2.jdbc.JdbcStatement) Statement(java.sql.Statement) GridSqlStatement(org.apache.ignite.internal.processors.query.h2.sql.GridSqlStatement) Connection(java.sql.Connection) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) IgniteSystemProperties.getString(org.apache.ignite.IgniteSystemProperties.getString)

Example 62 with Table

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

the class UpdatePlanBuilder method planForBulkLoad.

/**
 * Prepare update plan for COPY command (AKA bulk load).
 *
 * @param cmd Bulk load command
 * @return The update plan for this command.
 * @throws IgniteCheckedException if failed.
 */
@SuppressWarnings("ConstantConditions")
public static UpdatePlan planForBulkLoad(SqlBulkLoadCommand cmd, GridH2Table tbl) throws IgniteCheckedException {
    GridH2RowDescriptor desc = tbl.rowDescriptor();
    if (desc == null)
        throw new IgniteSQLException("Row descriptor undefined for table '" + tbl.getName() + "'", IgniteQueryErrorCode.NULL_TABLE_DESCRIPTOR);
    GridCacheContext<?, ?> cctx = desc.context();
    List<String> cols = cmd.columns();
    if (cols == null)
        throw new IgniteSQLException("Columns are not defined", IgniteQueryErrorCode.NULL_TABLE_DESCRIPTOR);
    String[] colNames = new String[cols.size()];
    int[] colTypes = new int[cols.size()];
    int keyColIdx = -1;
    int valColIdx = -1;
    boolean hasKeyProps = false;
    boolean hasValProps = false;
    for (int i = 0; i < cols.size(); i++) {
        String colName = cols.get(i);
        colNames[i] = colName;
        Column h2Col = tbl.getColumn(colName);
        colTypes[i] = h2Col.getType();
        int colId = h2Col.getColumnId();
        if (desc.isKeyColumn(colId)) {
            keyColIdx = i;
            continue;
        }
        if (desc.isValueColumn(colId)) {
            valColIdx = i;
            continue;
        }
        GridQueryProperty prop = desc.type().property(colName);
        assert prop != null : "Property '" + colName + "' not found.";
        if (prop.key())
            hasKeyProps = true;
        else
            hasValProps = true;
    }
    KeyValueSupplier keySupplier = createSupplier(cctx, desc.type(), keyColIdx, hasKeyProps, true, false);
    KeyValueSupplier valSupplier = createSupplier(cctx, desc.type(), valColIdx, hasValProps, false, false);
    return new UpdatePlan(UpdateMode.BULK_LOAD, tbl, colNames, colTypes, keySupplier, valSupplier, keyColIdx, valColIdx, null, true, null, 0, null, null);
}
Also used : GridQueryProperty(org.apache.ignite.internal.processors.query.GridQueryProperty) GridH2RowDescriptor(org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor) Column(org.h2.table.Column) GridSqlColumn(org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException)

Example 63 with Table

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

the class UpdatePlanBuilder method planForUpdate.

/**
 * Prepare update plan for UPDATE or DELETE.
 *
 * @param stmt UPDATE or DELETE statement.
 * @param loc Local query flag.
 * @param idx Indexing.
 * @param conn Connection.
 * @param fieldsQuery Original query.
 * @param errKeysPos index to inject param for re-run keys at. Null if it's not a re-run plan.
 * @return Update plan.
 * @throws IgniteCheckedException if failed.
 */
private static UpdatePlan planForUpdate(GridSqlStatement stmt, boolean loc, IgniteH2Indexing idx, @Nullable Connection conn, @Nullable SqlFieldsQuery fieldsQuery, @Nullable Integer errKeysPos) throws IgniteCheckedException {
    GridSqlElement target;
    FastUpdate fastUpdate;
    UpdateMode mode;
    if (stmt instanceof GridSqlUpdate) {
        // Let's verify that user is not trying to mess with key's columns directly
        verifyUpdateColumns(stmt);
        GridSqlUpdate update = (GridSqlUpdate) stmt;
        target = update.target();
        fastUpdate = DmlAstUtils.getFastUpdateArgs(update);
        mode = UpdateMode.UPDATE;
    } else if (stmt instanceof GridSqlDelete) {
        GridSqlDelete del = (GridSqlDelete) stmt;
        target = del.from();
        fastUpdate = DmlAstUtils.getFastDeleteArgs(del);
        mode = UpdateMode.DELETE;
    } else
        throw new IgniteSQLException("Unexpected DML operation [cls=" + stmt.getClass().getName() + ']', IgniteQueryErrorCode.UNEXPECTED_OPERATION);
    GridSqlTable tbl = DmlAstUtils.gridTableForElement(target);
    GridH2Table h2Tbl = tbl.dataTable();
    GridH2RowDescriptor desc = h2Tbl.rowDescriptor();
    if (desc == null)
        throw new IgniteSQLException("Row descriptor undefined for table '" + h2Tbl.getName() + "'", IgniteQueryErrorCode.NULL_TABLE_DESCRIPTOR);
    if (fastUpdate != null) {
        return new UpdatePlan(mode, h2Tbl, null, fastUpdate, null);
    } else {
        GridSqlSelect sel;
        if (stmt instanceof GridSqlUpdate) {
            List<GridSqlColumn> updatedCols = ((GridSqlUpdate) stmt).cols();
            int valColIdx = -1;
            String[] colNames = new String[updatedCols.size()];
            int[] colTypes = new int[updatedCols.size()];
            for (int i = 0; i < updatedCols.size(); i++) {
                colNames[i] = updatedCols.get(i).columnName();
                colTypes[i] = updatedCols.get(i).resultType().type();
                Column col = updatedCols.get(i).column();
                if (desc.isValueColumn(col.getColumnId()))
                    valColIdx = i;
            }
            boolean hasNewVal = (valColIdx != -1);
            // Statement updates distinct properties if it does not have _val in updated columns list
            // or if its list of updated columns includes only _val, i.e. is single element.
            boolean hasProps = !hasNewVal || updatedCols.size() > 1;
            // Index of new _val in results of SELECT
            if (hasNewVal)
                valColIdx += 2;
            int newValColIdx = (hasNewVal ? valColIdx : 1);
            KeyValueSupplier valSupplier = createSupplier(desc.context(), desc.type(), newValColIdx, hasProps, false, true);
            sel = DmlAstUtils.selectForUpdate((GridSqlUpdate) stmt, errKeysPos);
            String selectSql = sel.getSQL();
            DmlDistributedPlanInfo distributed = F.isEmpty(selectSql) ? null : checkPlanCanBeDistributed(idx, conn, fieldsQuery, loc, selectSql, tbl.dataTable().cacheName());
            return new UpdatePlan(UpdateMode.UPDATE, h2Tbl, colNames, colTypes, null, valSupplier, -1, valColIdx, selectSql, false, null, 0, null, distributed);
        } else {
            sel = DmlAstUtils.selectForDelete((GridSqlDelete) stmt, errKeysPos);
            String selectSql = sel.getSQL();
            DmlDistributedPlanInfo distributed = F.isEmpty(selectSql) ? null : checkPlanCanBeDistributed(idx, conn, fieldsQuery, loc, selectSql, tbl.dataTable().cacheName());
            return new UpdatePlan(UpdateMode.DELETE, h2Tbl, selectSql, null, distributed);
        }
    }
}
Also used : GridSqlDelete(org.apache.ignite.internal.processors.query.h2.sql.GridSqlDelete) GridSqlSelect(org.apache.ignite.internal.processors.query.h2.sql.GridSqlSelect) GridH2RowDescriptor(org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor) Column(org.h2.table.Column) GridSqlColumn(org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn) GridSqlTable(org.apache.ignite.internal.processors.query.h2.sql.GridSqlTable) GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table) GridSqlColumn(org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) GridSqlElement(org.apache.ignite.internal.processors.query.h2.sql.GridSqlElement) GridSqlUpdate(org.apache.ignite.internal.processors.query.h2.sql.GridSqlUpdate)

Example 64 with Table

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

the class GridH2CollocationModel method isAffinityColumn.

/**
 * @param f Table filter.
 * @param expCol Expression column.
 * @param validate Query validation flag.
 * @return {@code true} It it is an affinity column.
 */
private static boolean isAffinityColumn(TableFilter f, ExpressionColumn expCol, boolean validate) {
    Column col = expCol.getColumn();
    if (col == null)
        return false;
    Table t = col.getTable();
    if (t.isView()) {
        Query qry;
        if (f.getIndex() != null)
            qry = getSubQuery(f);
        else
            qry = GridSqlQueryParser.VIEW_QUERY.get((TableView) t);
        return isAffinityColumn(qry, expCol, validate);
    }
    if (t instanceof GridH2Table) {
        if (validate && ((GridH2Table) t).rowDescriptor().context().customAffinityMapper())
            throw customAffinityError(((GridH2Table) t).cacheName());
        IndexColumn affCol = ((GridH2Table) t).getAffinityKeyColumn();
        return affCol != null && col.getColumnId() == affCol.column.getColumnId();
    }
    return false;
}
Also used : Table(org.h2.table.Table) Query(org.h2.command.dml.Query) ExpressionColumn(org.h2.expression.ExpressionColumn) Column(org.h2.table.Column) IndexColumn(org.h2.table.IndexColumn) IndexColumn(org.h2.table.IndexColumn)

Example 65 with Table

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

the class GridH2CollocationModel 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;
        int maxMultiplier = MULTIPLIER_COLLOCATED;
        for (int i = 0; i < childFilters.length; i++) {
            GridH2CollocationModel child = child(i, true);
            Type t = child.type(true);
            if (child.multiplier == MULTIPLIER_REPLICATED_NOT_LAST)
                maxMultiplier = child.multiplier;
            if (t.isPartitioned()) {
                partitioned = true;
                if (!t.isCollocated()) {
                    collocated = false;
                    int m = child.multiplier(true);
                    if (m > maxMultiplier) {
                        maxMultiplier = m;
                        if (maxMultiplier == MULTIPLIER_REPLICATED_NOT_LAST)
                            break;
                    }
                }
            }
        }
        type = Type.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 = Type.REPLICATED;
            multiplier = MULTIPLIER_COLLOCATED;
            return;
        }
        // to all the affinity nodes the "base" does not need to get remote results.
        if (!upper.findPartitionedTableBefore(filter)) {
            type = Type.PARTITIONED_COLLOCATED;
            multiplier = MULTIPLIER_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 = Type.PARTITIONED_COLLOCATED;
                    multiplier = MULTIPLIER_COLLOCATED;
                    break;
                case HAS_AFFINITY_CONDITION:
                    type = Type.PARTITIONED_NOT_COLLOCATED;
                    multiplier = MULTIPLIER_UNICAST;
                    break;
                case NONE:
                    type = Type.PARTITIONED_NOT_COLLOCATED;
                    multiplier = MULTIPLIER_BROADCAST;
                    break;
                default:
                    throw new IllegalStateException();
            }
        }
        if (upper.previousReplicated(filter))
            multiplier = MULTIPLIER_REPLICATED_NOT_LAST;
    }
}
Also used : Table(org.h2.table.Table)

Aggregations

Column (org.h2.table.Column)20 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)18 Statement (java.sql.Statement)13 SQLException (java.sql.SQLException)12 GridH2Table (org.apache.ignite.internal.processors.query.h2.opt.GridH2Table)12 IndexColumn (org.h2.table.IndexColumn)11 ResultSet (java.sql.ResultSet)9 IgniteException (org.apache.ignite.IgniteException)9 GridH2RowDescriptor (org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor)8 AlterTableAddConstraint (org.h2.command.ddl.AlterTableAddConstraint)8 SimpleResultSet (org.h2.tools.SimpleResultSet)8 GridSqlColumn (org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn)7 Table (org.h2.table.Table)7 Connection (java.sql.Connection)6 HashSet (java.util.HashSet)6 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)6 GridSqlElement (org.apache.ignite.internal.processors.query.h2.sql.GridSqlElement)6 Prepared (org.h2.command.Prepared)6 ValueString (org.h2.value.ValueString)6 PreparedStatement (java.sql.PreparedStatement)5