Search in sources :

Example 1 with DbException

use of org.gridgain.internal.h2.message.DbException in project ignite by apache.

the class ValidateIndexesClosure method processIndex.

/**
 * @param cacheCtxWithIdx Cache context and appropriate index.
 * @param idleChecker Idle check closure.
 */
private Map<String, ValidateIndexesPartitionResult> processIndex(T2<GridCacheContext, Index> cacheCtxWithIdx, IgniteInClosure<Integer> idleChecker) {
    if (validateCtx.isCancelled())
        return emptyMap();
    GridCacheContext ctx = cacheCtxWithIdx.get1();
    Index idx = cacheCtxWithIdx.get2();
    ValidateIndexesPartitionResult idxValidationRes = new ValidateIndexesPartitionResult();
    boolean enoughIssues = false;
    Cursor cursor = null;
    try (Session session = mvccSession(cacheCtxWithIdx.get1())) {
        cursor = idx.find(session, null, null);
        if (cursor == null)
            throw new IgniteCheckedException("Can't iterate through index: " + idx);
    } catch (Throwable t) {
        IndexValidationIssue is = new IndexValidationIssue(null, ctx.name(), idx.getName(), t);
        log.error("Find in index failed: " + is.toString());
        idxValidationRes.reportIssue(is);
        enoughIssues = true;
    }
    final boolean skipConditions = checkFirst > 0 || checkThrough > 0;
    final boolean bothSkipConditions = checkFirst > 0 && checkThrough > 0;
    long current = 0;
    long processedNumber = 0;
    KeyCacheObject previousKey = null;
    while (!enoughIssues && !validateCtx.isCancelled()) {
        KeyCacheObject h2key = null;
        try {
            try {
                if (!cursor.next())
                    break;
            } catch (DbException e) {
                if (X.hasCause(e, CorruptedTreeException.class))
                    throw new IgniteCheckedException("Key is present in SQL index, but is missing in corresponding " + "data page. Previous successfully read key: " + CacheObjectUtils.unwrapBinaryIfNeeded(ctx.cacheObjectContext(), previousKey, true, true), X.cause(e, CorruptedTreeException.class));
                throw e;
            }
            H2CacheRow h2Row = (H2CacheRow) cursor.get();
            if (skipConditions) {
                if (bothSkipConditions) {
                    if (processedNumber > checkFirst)
                        break;
                    else if (current++ % checkThrough > 0)
                        continue;
                    else
                        processedNumber++;
                } else {
                    if (checkFirst > 0) {
                        if (current++ > checkFirst)
                            break;
                    } else {
                        if (current++ % checkThrough > 0)
                            continue;
                    }
                }
            }
            h2key = h2Row.key();
            if (h2Row.link() != 0L) {
                CacheDataRow cacheDataStoreRow = ctx.group().offheap().read(ctx, h2key);
                if (cacheDataStoreRow == null)
                    throw new IgniteCheckedException("Key is present in SQL index, but can't be found in CacheDataTree.");
            } else
                throw new IgniteCheckedException("Invalid index row, possibly deleted " + h2Row);
        } catch (Throwable t) {
            Object o = CacheObjectUtils.unwrapBinaryIfNeeded(ctx.cacheObjectContext(), h2key, true, true);
            IndexValidationIssue is = new IndexValidationIssue(String.valueOf(o), ctx.name(), idx.getName(), t);
            log.error("Failed to lookup key: " + is.toString());
            enoughIssues |= idxValidationRes.reportIssue(is);
        } finally {
            previousKey = h2key;
        }
    }
    CacheGroupContext group = ctx.group();
    String uniqueIdxName = String.format("[cacheGroup=%s, cacheGroupId=%s, cache=%s, cacheId=%s, idx=%s]", group.name(), group.groupId(), ctx.name(), ctx.cacheId(), idx.getName());
    idleChecker.apply(group.groupId());
    processedIndexes.incrementAndGet();
    printProgressOfIndexValidationIfNeeded();
    return Collections.singletonMap(uniqueIdxName, idxValidationRes);
}
Also used : CacheDataRow(org.apache.ignite.internal.processors.cache.persistence.CacheDataRow) GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) Index(org.h2.index.Index) H2CacheRow(org.apache.ignite.internal.processors.query.h2.opt.H2CacheRow) Cursor(org.h2.index.Cursor) DbException(org.h2.message.DbException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) CorruptedTreeException(org.apache.ignite.internal.processors.cache.persistence.tree.CorruptedTreeException) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) CacheGroupContext(org.apache.ignite.internal.processors.cache.CacheGroupContext) Session(org.h2.engine.Session) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject)

Example 2 with DbException

use of org.gridgain.internal.h2.message.DbException in project h2database by h2database.

the class Parser method parseColumnWithType.

private Column parseColumnWithType(String columnName) {
    String original = currentToken;
    boolean regular = false;
    int originalScale = -1;
    if (readIf("LONG")) {
        if (readIf("RAW")) {
            original += " RAW";
        }
    } else if (readIf("DOUBLE")) {
        if (readIf("PRECISION")) {
            original += " PRECISION";
        }
    } else if (readIf("CHARACTER")) {
        if (readIf("VARYING")) {
            original += " VARYING";
        }
    } else if (readIf("TIME")) {
        if (readIf("(")) {
            originalScale = readPositiveInt();
            if (originalScale > ValueTime.MAXIMUM_SCALE) {
                throw DbException.get(ErrorCode.INVALID_VALUE_SCALE_PRECISION, Integer.toString(originalScale));
            }
            read(")");
        }
        if (readIf("WITHOUT")) {
            read("TIME");
            read("ZONE");
            original += " WITHOUT TIME ZONE";
        }
    } else if (readIf("TIMESTAMP")) {
        if (readIf("(")) {
            originalScale = readPositiveInt();
            // Allow non-standard TIMESTAMP(..., ...) syntax
            if (readIf(",")) {
                originalScale = readPositiveInt();
            }
            if (originalScale > ValueTimestamp.MAXIMUM_SCALE) {
                throw DbException.get(ErrorCode.INVALID_VALUE_SCALE_PRECISION, Integer.toString(originalScale));
            }
            read(")");
        }
        if (readIf("WITH")) {
            read("TIME");
            read("ZONE");
            original += " WITH TIME ZONE";
        } else if (readIf("WITHOUT")) {
            read("TIME");
            read("ZONE");
            original += " WITHOUT TIME ZONE";
        }
    } else {
        regular = true;
    }
    long precision = -1;
    int displaySize = -1;
    String[] enumerators = null;
    int scale = -1;
    String comment = null;
    Column templateColumn = null;
    DataType dataType;
    if (!identifiersToUpper) {
        original = StringUtils.toUpperEnglish(original);
    }
    UserDataType userDataType = database.findUserDataType(original);
    if (userDataType != null) {
        templateColumn = userDataType.getColumn();
        dataType = DataType.getDataType(templateColumn.getType());
        comment = templateColumn.getComment();
        original = templateColumn.getOriginalSQL();
        precision = templateColumn.getPrecision();
        displaySize = templateColumn.getDisplaySize();
        scale = templateColumn.getScale();
        enumerators = templateColumn.getEnumerators();
    } else {
        Mode mode = database.getMode();
        dataType = DataType.getTypeByName(original, mode);
        if (dataType == null || mode.disallowedTypes.contains(original)) {
            throw DbException.get(ErrorCode.UNKNOWN_DATA_TYPE_1, currentToken);
        }
    }
    if (database.getIgnoreCase() && dataType.type == Value.STRING && !equalsToken("VARCHAR_CASESENSITIVE", original)) {
        original = "VARCHAR_IGNORECASE";
        dataType = DataType.getTypeByName(original, database.getMode());
    }
    if (regular) {
        read();
    }
    precision = precision == -1 ? dataType.defaultPrecision : precision;
    displaySize = displaySize == -1 ? dataType.defaultDisplaySize : displaySize;
    scale = scale == -1 ? dataType.defaultScale : scale;
    if (dataType.supportsPrecision || dataType.supportsScale) {
        int t = dataType.type;
        if (t == Value.TIME || t == Value.TIMESTAMP || t == Value.TIMESTAMP_TZ) {
            if (originalScale >= 0) {
                scale = originalScale;
                switch(t) {
                    case Value.TIME:
                        if (original.equals("TIME WITHOUT TIME ZONE")) {
                            original = "TIME(" + originalScale + ") WITHOUT TIME ZONE";
                        } else {
                            original = original + '(' + originalScale + ')';
                        }
                        precision = displaySize = ValueTime.getDisplaySize(originalScale);
                        break;
                    case Value.TIMESTAMP:
                        if (original.equals("TIMESTAMP WITHOUT TIME ZONE")) {
                            original = "TIMESTAMP(" + originalScale + ") WITHOUT TIME ZONE";
                        } else {
                            original = original + '(' + originalScale + ')';
                        }
                        precision = displaySize = ValueTimestamp.getDisplaySize(originalScale);
                        break;
                    case Value.TIMESTAMP_TZ:
                        original = "TIMESTAMP(" + originalScale + ") WITH TIME ZONE";
                        precision = displaySize = ValueTimestampTimeZone.getDisplaySize(originalScale);
                        break;
                }
            }
        } else if (readIf("(")) {
            if (!readIf("MAX")) {
                long p = readLong();
                if (readIf("K")) {
                    p *= 1024;
                } else if (readIf("M")) {
                    p *= 1024 * 1024;
                } else if (readIf("G")) {
                    p *= 1024 * 1024 * 1024;
                }
                if (p > Long.MAX_VALUE) {
                    p = Long.MAX_VALUE;
                }
                original += "(" + p;
                // Oracle syntax
                if (!readIf("CHAR")) {
                    readIf("BYTE");
                }
                if (dataType.supportsScale) {
                    if (readIf(",")) {
                        scale = readInt();
                        original += ", " + scale;
                    } else {
                        scale = 0;
                    }
                }
                precision = p;
                displaySize = MathUtils.convertLongToInt(precision);
                original += ")";
            }
            read(")");
        }
    } else if (dataType.type == Value.DOUBLE && original.equals("FLOAT")) {
        if (readIf("(")) {
            int p = readPositiveInt();
            read(")");
            if (p > 53) {
                throw DbException.get(ErrorCode.INVALID_VALUE_SCALE_PRECISION, Integer.toString(p));
            }
            if (p <= 24) {
                dataType = DataType.getDataType(Value.FLOAT);
            }
            original = original + '(' + p + ')';
        }
    } else if (dataType.type == Value.ENUM) {
        if (readIf("(")) {
            java.util.List<String> enumeratorList = new ArrayList<>();
            original += '(';
            String enumerator0 = readString();
            enumeratorList.add(enumerator0);
            original += "'" + enumerator0 + "'";
            while (readIfMore(true)) {
                original += ',';
                String enumeratorN = readString();
                original += "'" + enumeratorN + "'";
                enumeratorList.add(enumeratorN);
            }
            original += ')';
            enumerators = enumeratorList.toArray(new String[0]);
        }
        try {
            ValueEnum.check(enumerators);
        } catch (DbException e) {
            throw e.addSQL(original);
        }
    } else if (readIf("(")) {
        // Support for MySQL: INT(11), MEDIUMINT(8) and so on.
        // Just ignore the precision.
        readPositiveInt();
        read(")");
    }
    if (readIf("FOR")) {
        read("BIT");
        read("DATA");
        if (dataType.type == Value.STRING) {
            dataType = DataType.getTypeByName("BINARY", database.getMode());
        }
    }
    // MySQL compatibility
    readIf("UNSIGNED");
    int type = dataType.type;
    if (scale > precision) {
        throw DbException.get(ErrorCode.INVALID_VALUE_SCALE_PRECISION, Integer.toString(scale), Long.toString(precision));
    }
    Column column = new Column(columnName, type, precision, scale, displaySize, enumerators);
    if (templateColumn != null) {
        column.setNullable(templateColumn.isNullable());
        column.setDefaultExpression(session, templateColumn.getDefaultExpression());
        int selectivity = templateColumn.getSelectivity();
        if (selectivity != Constants.SELECTIVITY_DEFAULT) {
            column.setSelectivity(selectivity);
        }
        Expression checkConstraint = templateColumn.getCheckConstraint(session, columnName);
        column.addCheckConstraint(session, checkConstraint);
    }
    column.setComment(comment);
    column.setOriginalSQL(original);
    return column;
}
Also used : CreateUserDataType(org.h2.command.ddl.CreateUserDataType) DropUserDataType(org.h2.command.ddl.DropUserDataType) UserDataType(org.h2.engine.UserDataType) Mode(org.h2.engine.Mode) CompareMode(org.h2.value.CompareMode) ArrayList(java.util.ArrayList) ValueString(org.h2.value.ValueString) AlterTableRenameConstraint(org.h2.command.ddl.AlterTableRenameConstraint) AlterTableAddConstraint(org.h2.command.ddl.AlterTableAddConstraint) AlterTableDropConstraint(org.h2.command.ddl.AlterTableDropConstraint) DbException(org.h2.message.DbException) AlterTableRenameColumn(org.h2.command.ddl.AlterTableRenameColumn) AlterTableAlterColumn(org.h2.command.ddl.AlterTableAlterColumn) Column(org.h2.table.Column) ExpressionColumn(org.h2.expression.ExpressionColumn) IndexColumn(org.h2.table.IndexColumn) Expression(org.h2.expression.Expression) ValueExpression(org.h2.expression.ValueExpression) CreateUserDataType(org.h2.command.ddl.CreateUserDataType) DataType(org.h2.value.DataType) DropUserDataType(org.h2.command.ddl.DropUserDataType) UserDataType(org.h2.engine.UserDataType)

Example 3 with DbException

use of org.gridgain.internal.h2.message.DbException in project h2database by h2database.

the class Insert method addRowImpl.

private Row addRowImpl(Value[] values) {
    Row newRow = table.getTemplateRow();
    setCurrentRowNumber(++rowNumber);
    for (int j = 0, len = columns.length; j < len; j++) {
        Column c = columns[j];
        int index = c.getColumnId();
        try {
            Value v = c.convert(values[j], session.getDatabase().getMode());
            newRow.setValue(index, v);
        } catch (DbException ex) {
            throw setRow(ex, rowNumber, getSQL(values));
        }
    }
    table.validateConvertUpdateSequence(session, newRow);
    boolean done = table.fireBeforeRow(session, null, newRow);
    if (!done) {
        table.addRow(session, newRow);
        session.log(table, UndoLogRecord.INSERT, newRow);
        table.fireAfterRow(session, null, newRow, false);
        return newRow;
    }
    return null;
}
Also used : Column(org.h2.table.Column) ExpressionColumn(org.h2.expression.ExpressionColumn) Value(org.h2.value.Value) SequenceValue(org.h2.expression.SequenceValue) Row(org.h2.result.Row) DbException(org.h2.message.DbException)

Example 4 with DbException

use of org.gridgain.internal.h2.message.DbException in project h2database by h2database.

the class Replace method update.

@Override
public int update() {
    int count;
    session.getUser().checkRight(table, Right.INSERT);
    session.getUser().checkRight(table, Right.UPDATE);
    setCurrentRowNumber(0);
    if (!list.isEmpty()) {
        count = 0;
        for (int x = 0, size = list.size(); x < size; x++) {
            setCurrentRowNumber(x + 1);
            Expression[] expr = list.get(x);
            Row newRow = table.getTemplateRow();
            for (int i = 0, len = columns.length; i < len; i++) {
                Column c = columns[i];
                int index = c.getColumnId();
                Expression e = expr[i];
                if (e != null) {
                    // e can be null (DEFAULT)
                    try {
                        Value v = c.convert(e.getValue(session));
                        newRow.setValue(index, v);
                    } catch (DbException ex) {
                        throw setRow(ex, count, getSQL(expr));
                    }
                }
            }
            replace(newRow);
            count++;
        }
    } else {
        ResultInterface rows = query.query(0);
        count = 0;
        table.fire(session, Trigger.UPDATE | Trigger.INSERT, true);
        table.lock(session, true, false);
        while (rows.next()) {
            count++;
            Value[] r = rows.currentRow();
            Row newRow = table.getTemplateRow();
            setCurrentRowNumber(count);
            for (int j = 0; j < columns.length; j++) {
                Column c = columns[j];
                int index = c.getColumnId();
                try {
                    Value v = c.convert(r[j]);
                    newRow.setValue(index, v);
                } catch (DbException ex) {
                    throw setRow(ex, count, getSQL(r));
                }
            }
            replace(newRow);
        }
        rows.close();
        table.fire(session, Trigger.UPDATE | Trigger.INSERT, false);
    }
    return count;
}
Also used : ResultInterface(org.h2.result.ResultInterface) Expression(org.h2.expression.Expression) Column(org.h2.table.Column) Value(org.h2.value.Value) Row(org.h2.result.Row) DbException(org.h2.message.DbException)

Example 5 with DbException

use of org.gridgain.internal.h2.message.DbException in project h2database by h2database.

the class Database method throwLastBackgroundException.

private void throwLastBackgroundException() {
    if (backgroundException != null) {
        // we don't care too much about concurrency here,
        // we just want to make sure the exception is _normally_
        // not just logged to the .trace.db file
        DbException b = backgroundException;
        backgroundException = null;
        if (b != null) {
            // wrap the exception, so we see it was thrown here
            throw DbException.get(b.getErrorCode(), b, b.getMessage());
        }
    }
}
Also used : DbException(org.h2.message.DbException)

Aggregations

DbException (org.h2.message.DbException)138 DbException (org.gridgain.internal.h2.message.DbException)56 SQLException (java.sql.SQLException)32 Value (org.h2.value.Value)30 IOException (java.io.IOException)25 ArrayList (java.util.ArrayList)24 Constraint (org.h2.constraint.Constraint)24 Schema (org.h2.schema.Schema)22 ValueBigint (org.h2.value.ValueBigint)20 Database (org.h2.engine.Database)17 Row (org.h2.result.Row)17 Column (org.h2.table.Column)16 Value (org.gridgain.internal.h2.value.Value)15 FunctionAlias (org.h2.schema.FunctionAlias)14 JavaMethod (org.h2.schema.FunctionAlias.JavaMethod)14 Expression (org.h2.expression.Expression)13 Index (org.h2.index.Index)13 TypeInfo (org.h2.value.TypeInfo)13 ValueExpression (org.h2.expression.ValueExpression)12 UserDefinedFunction (org.h2.schema.UserDefinedFunction)12