Search in sources :

Example 11 with Column

use of org.gridgain.internal.h2.table.Column in project ignite by apache.

the class DmlAstUtils method selectForDelete.

/**
 * Generate SQL SELECT based on DELETE's WHERE, LIMIT, etc.
 *
 * @param del Delete statement.
 * @return SELECT statement.
 */
public static GridSqlSelect selectForDelete(GridSqlDelete del) {
    GridSqlSelect mapQry = new GridSqlSelect();
    mapQry.from(del.from());
    Set<GridSqlTable> tbls = new HashSet<>();
    collectAllGridTablesInTarget(del.from(), tbls);
    assert tbls.size() == 1 : "Failed to determine target table for DELETE";
    GridSqlTable tbl = tbls.iterator().next();
    GridH2Table gridTbl = tbl.dataTable();
    assert gridTbl != null : "Failed to determine target grid table for DELETE";
    Column h2KeyCol = gridTbl.getColumn(QueryUtils.KEY_COL);
    Column h2ValCol = gridTbl.getColumn(QueryUtils.VAL_COL);
    GridSqlColumn keyCol = new GridSqlColumn(h2KeyCol, tbl, h2KeyCol.getName());
    keyCol.resultType(GridSqlType.fromColumn(h2KeyCol));
    GridSqlColumn valCol = new GridSqlColumn(h2ValCol, tbl, h2ValCol.getName());
    valCol.resultType(GridSqlType.fromColumn(h2ValCol));
    mapQry.addColumn(keyCol, true);
    mapQry.addColumn(valCol, true);
    GridSqlElement where = del.where();
    mapQry.where(where);
    mapQry.limit(del.limit());
    return mapQry;
}
Also used : GridSqlColumn(org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn) Column(org.h2.table.Column) 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) GridSqlElement(org.apache.ignite.internal.processors.query.h2.sql.GridSqlElement) GridSqlSelect(org.apache.ignite.internal.processors.query.h2.sql.GridSqlSelect) HashSet(java.util.HashSet)

Example 12 with Column

use of org.gridgain.internal.h2.table.Column in project ignite by apache.

the class DmlAstUtils method selectForUpdate.

/**
 * Generate SQL SELECT based on UPDATE's WHERE, LIMIT, etc.
 *
 * @param update Update statement.
 * @return SELECT statement.
 */
public static GridSqlSelect selectForUpdate(GridSqlUpdate update) {
    GridSqlSelect mapQry = new GridSqlSelect();
    mapQry.from(update.target());
    Set<GridSqlTable> tbls = new HashSet<>();
    collectAllGridTablesInTarget(update.target(), tbls);
    assert tbls.size() == 1 : "Failed to determine target table for UPDATE";
    GridSqlTable tbl = tbls.iterator().next();
    GridH2Table gridTbl = tbl.dataTable();
    assert gridTbl != null : "Failed to determine target grid table for UPDATE";
    Column h2KeyCol = gridTbl.getColumn(QueryUtils.KEY_COL);
    Column h2ValCol = gridTbl.getColumn(QueryUtils.VAL_COL);
    GridSqlColumn keyCol = new GridSqlColumn(h2KeyCol, tbl, h2KeyCol.getName());
    keyCol.resultType(GridSqlType.fromColumn(h2KeyCol));
    GridSqlColumn valCol = new GridSqlColumn(h2ValCol, tbl, h2ValCol.getName());
    valCol.resultType(GridSqlType.fromColumn(h2ValCol));
    mapQry.addColumn(keyCol, true);
    mapQry.addColumn(valCol, true);
    for (GridSqlColumn c : update.cols()) {
        String newColName = Parser.quoteIdentifier("_upd_" + c.columnName());
        // We have to use aliases to cover cases when the user
        // wants to update _val field directly (if it's a literal)
        GridSqlAlias alias = new GridSqlAlias(newColName, elementOrDefault(update.set().get(c.columnName()), c), true);
        alias.resultType(c.resultType());
        mapQry.addColumn(alias, true);
    }
    GridSqlElement where = update.where();
    // On no MVCC mode we cannot use lazy mode when UPDATE query contains index with updated columns
    // and that index may be chosen to scan by WHERE condition
    // because in this case any rows update may be updated several times.
    // e.g. in the cases below we cannot use lazy mode:
    // 
    // 1. CREATE INDEX idx on test(val)
    // UPDATE test SET val = val + 1 WHERE val >= ?
    // 
    // 2. CREATE INDEX idx on test(val0, val1)
    // UPDATE test SET val1 = val1 + 1 WHERE val0 >= ?
    mapQry.canBeLazy(!isIndexWithUpdateColumnsMayBeUsed(gridTbl, update.cols().stream().map(GridSqlColumn::column).collect(Collectors.toSet()), extractColumns(gridTbl, where)));
    mapQry.where(where);
    mapQry.limit(update.limit());
    return mapQry;
}
Also used : GridSqlAlias(org.apache.ignite.internal.processors.query.h2.sql.GridSqlAlias) GridSqlColumn(org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn) Column(org.h2.table.Column) 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) GridSqlElement(org.apache.ignite.internal.processors.query.h2.sql.GridSqlElement) ValueString(org.h2.value.ValueString) GridSqlSelect(org.apache.ignite.internal.processors.query.h2.sql.GridSqlSelect) HashSet(java.util.HashSet)

Example 13 with Column

use of org.gridgain.internal.h2.table.Column in project ignite by apache.

the class GridH2Table method addColumns.

/**
 * Add new columns to this table.
 *
 * @param cols Columns to add.
 * @param ifNotExists Ignore this command if {@code cols} has size of 1 and column with given name already exists.
 */
public void addColumns(List<QueryField> cols, boolean ifNotExists) {
    assert !ifNotExists || cols.size() == 1;
    lock(true);
    try {
        Column[] safeColumns0 = safeColumns;
        int pos = safeColumns0.length;
        Column[] newCols = new Column[safeColumns0.length + cols.size()];
        // First, let's copy existing columns to new array
        System.arraycopy(safeColumns0, 0, newCols, 0, safeColumns0.length);
        // And now, let's add new columns
        for (QueryField col : cols) {
            if (doesColumnExist(col.name())) {
                if (ifNotExists && cols.size() == 1)
                    return;
                else
                    throw new IgniteSQLException("Column already exists [tblName=" + getName() + ", colName=" + col.name() + ']');
            }
            try {
                Column c = new Column(col.name(), DataType.getTypeFromClass(Class.forName(col.typeName())));
                c.setNullable(col.isNullable());
                newCols[pos++] = c;
            } catch (ClassNotFoundException e) {
                throw new IgniteSQLException("H2 data type not found for class: " + col.typeName(), e);
            }
        }
        setColumns(newCols);
        desc.refreshMetadataFromTypeDescriptor();
        incrementModificationCounter();
    } finally {
        unlock(true);
    }
}
Also used : QueryField(org.apache.ignite.internal.processors.query.QueryField) IndexColumn(org.h2.table.IndexColumn) Column(org.h2.table.Column) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException)

Example 14 with Column

use of org.gridgain.internal.h2.table.Column in project ignite by apache.

the class GridH2Table method dropColumns.

/**
 * Drop columns.
 *
 * @param cols Columns.
 * @param ifExists If EXISTS flag.
 */
@SuppressWarnings("ForLoopReplaceableByForEach")
public void dropColumns(List<String> cols, boolean ifExists) {
    assert !ifExists || cols.size() == 1;
    lock(true);
    try {
        Column[] safeColumns0 = safeColumns;
        int size = safeColumns0.length;
        for (String name : cols) {
            if (!doesColumnExist(name)) {
                if (ifExists && cols.size() == 1)
                    return;
                else
                    throw new IgniteSQLException("Column does not exist [tblName=" + getName() + ", colName=" + name + ']');
            }
            size--;
        }
        assert size > QueryUtils.DEFAULT_COLUMNS_COUNT;
        Column[] newCols = new Column[size];
        int dst = 0;
        for (int i = 0; i < safeColumns0.length; i++) {
            Column column = safeColumns0[i];
            for (String name : cols) {
                if (F.eq(name, column.getName())) {
                    column = null;
                    break;
                }
            }
            if (column != null)
                newCols[dst++] = column;
        }
        setColumns(newCols);
        desc.refreshMetadataFromTypeDescriptor();
        for (Index idx : getIndexes()) {
            if (idx instanceof GridH2IndexBase)
                ((GridH2IndexBase) idx).refreshColumnIds();
        }
        incrementModificationCounter();
    } finally {
        unlock(true);
    }
}
Also used : IndexColumn(org.h2.table.IndexColumn) Column(org.h2.table.Column) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) Index(org.h2.index.Index) H2TreeIndex(org.apache.ignite.internal.processors.query.h2.database.H2TreeIndex) SpatialIndex(org.h2.index.SpatialIndex)

Example 15 with Column

use of org.gridgain.internal.h2.table.Column in project ignite by apache.

the class H2Utils method unwrapKeyColumns.

/**
 * Create list of index columns. Where possible _KEY columns will be unwrapped.
 *
 * @param tbl GridH2Table instance
 * @param idxCols List of index columns.
 *
 * @return Array of key and affinity columns. Key's, if it possible, splitted into simple components.
 */
@NotNull
public static IndexColumn[] unwrapKeyColumns(GridH2Table tbl, IndexColumn[] idxCols) {
    ArrayList<IndexColumn> keyCols = new ArrayList<>();
    boolean isSql = tbl.rowDescriptor().tableDescriptor().sql();
    if (!isSql)
        return idxCols;
    GridQueryTypeDescriptor type = tbl.rowDescriptor().type();
    for (IndexColumn idxCol : idxCols) {
        if (idxCol.column.getColumnId() == KEY_COL) {
            if (QueryUtils.isSqlType(type.keyClass())) {
                int altKeyColId = tbl.rowDescriptor().getAlternativeColumnId(QueryUtils.KEY_COL);
                // Remap simple key to alternative column.
                IndexColumn idxKeyCol = new IndexColumn();
                idxKeyCol.column = tbl.getColumn(altKeyColId);
                idxKeyCol.columnName = idxKeyCol.column.getName();
                idxKeyCol.sortType = idxCol.sortType;
                keyCols.add(idxKeyCol);
            } else {
                boolean added = false;
                for (String propName : type.fields().keySet()) {
                    GridQueryProperty prop = type.property(propName);
                    if (prop.key()) {
                        added = true;
                        Column col = tbl.getColumn(propName);
                        keyCols.add(tbl.indexColumn(col.getColumnId(), SortOrder.ASCENDING));
                    }
                }
                // we have to fall back to whole-key index.
                if (!added)
                    keyCols.add(idxCol);
            }
        } else
            keyCols.add(idxCol);
    }
    return keyCols.toArray(new IndexColumn[0]);
}
Also used : GridQueryProperty(org.apache.ignite.internal.processors.query.GridQueryProperty) IndexColumn(org.h2.table.IndexColumn) Column(org.h2.table.Column) ArrayList(java.util.ArrayList) ValueString(org.h2.value.ValueString) IndexColumn(org.h2.table.IndexColumn) GridQueryTypeDescriptor(org.apache.ignite.internal.processors.query.GridQueryTypeDescriptor) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

Column (org.h2.table.Column)300 IndexColumn (org.h2.table.IndexColumn)156 Column (org.gridgain.internal.h2.table.Column)150 ExpressionColumn (org.h2.expression.ExpressionColumn)117 Expression (org.h2.expression.Expression)94 Value (org.gridgain.internal.h2.value.Value)88 IndexColumn (org.gridgain.internal.h2.table.IndexColumn)82 ArrayList (java.util.ArrayList)81 DbException (org.gridgain.internal.h2.message.DbException)70 SQLException (java.sql.SQLException)69 AlterTableAlterColumn (org.h2.command.ddl.AlterTableAlterColumn)68 Value (org.h2.value.Value)63 ExpressionColumn (org.gridgain.internal.h2.expression.ExpressionColumn)59 AlterTableRenameColumn (org.h2.command.ddl.AlterTableRenameColumn)59 Table (org.h2.table.Table)52 Expression (org.gridgain.internal.h2.expression.Expression)50 ValueExpression (org.h2.expression.ValueExpression)48 Index (org.h2.index.Index)46 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)40 Database (org.h2.engine.Database)35