Search in sources :

Example 1 with DelegateSQLException

use of org.apache.phoenix.schema.DelegateSQLException in project phoenix by apache.

the class ColumnDef method validateDefault.

public boolean validateDefault(StatementContext context, PrimaryKeyConstraint pkConstraint) throws SQLException {
    String defaultStr = this.getExpression();
    if (defaultStr == null) {
        return true;
    }
    ExpressionCompiler compiler = new ExpressionCompiler(context);
    ParseNode defaultParseNode = new SQLParser(this.getExpression()).parseExpression();
    Expression defaultExpression = defaultParseNode.accept(compiler);
    if (!defaultParseNode.isStateless() || defaultExpression.getDeterminism() != Determinism.ALWAYS) {
        throw new SQLExceptionInfo.Builder(SQLExceptionCode.CANNOT_CREATE_DEFAULT).setColumnName(this.getColumnDefName().getColumnName()).build().buildException();
    }
    if (this.isRowTimestamp() || (pkConstraint != null && pkConstraint.isColumnRowTimestamp(this.getColumnDefName()))) {
        throw new SQLExceptionInfo.Builder(SQLExceptionCode.CANNOT_CREATE_DEFAULT_ROWTIMESTAMP).setColumnName(this.getColumnDefName().getColumnName()).build().buildException();
    }
    ImmutableBytesWritable ptr = new ImmutableBytesWritable();
    // Evaluate the expression to confirm it's validity
    LiteralExpression defaultValue = ExpressionUtil.getConstantExpression(defaultExpression, ptr);
    // A DEFAULT that evaluates to null should be ignored as it adds nothing
    if (defaultValue.getValue() == null) {
        return false;
    }
    PDataType sourceType = defaultExpression.getDataType();
    PDataType targetType = this.getDataType();
    // Ensure that coercion works (will throw if not)
    context.getTempPtr().set(ptr.get(), ptr.getOffset(), ptr.getLength());
    try {
        targetType.coerceBytes(context.getTempPtr(), defaultValue.getValue(), sourceType, defaultValue.getMaxLength(), defaultValue.getScale(), defaultValue.getSortOrder(), this.getMaxLength(), this.getScale(), this.getSortOrder());
    } catch (ConstraintViolationException e) {
        if (e.getCause() instanceof SQLException) {
            SQLException sqlE = (SQLException) e.getCause();
            throw new DelegateSQLException(sqlE, ". DEFAULT " + SQLExceptionInfo.COLUMN_NAME + "=" + this.getColumnDefName().getColumnName());
        }
        throw e;
    }
    if (!targetType.isSizeCompatible(ptr, defaultValue.getValue(), sourceType, sortOrder, defaultValue.getMaxLength(), defaultValue.getScale(), this.getMaxLength(), this.getScale())) {
        throw new SQLExceptionInfo.Builder(SQLExceptionCode.DATA_EXCEEDS_MAX_CAPACITY).setColumnName(this.getColumnDefName().getColumnName()).setMessage("DEFAULT " + this.getExpression()).build().buildException();
    }
    return true;
}
Also used : ImmutableBytesWritable(org.apache.hadoop.hbase.io.ImmutableBytesWritable) SQLException(java.sql.SQLException) DelegateSQLException(org.apache.phoenix.schema.DelegateSQLException) LiteralExpression(org.apache.phoenix.expression.LiteralExpression) DelegateSQLException(org.apache.phoenix.schema.DelegateSQLException) PDataType(org.apache.phoenix.schema.types.PDataType) LiteralExpression(org.apache.phoenix.expression.LiteralExpression) Expression(org.apache.phoenix.expression.Expression) ConstraintViolationException(org.apache.phoenix.schema.ConstraintViolationException) ExpressionCompiler(org.apache.phoenix.compile.ExpressionCompiler) SQLExceptionInfo(org.apache.phoenix.exception.SQLExceptionInfo)

Aggregations

SQLException (java.sql.SQLException)1 ImmutableBytesWritable (org.apache.hadoop.hbase.io.ImmutableBytesWritable)1 ExpressionCompiler (org.apache.phoenix.compile.ExpressionCompiler)1 SQLExceptionInfo (org.apache.phoenix.exception.SQLExceptionInfo)1 Expression (org.apache.phoenix.expression.Expression)1 LiteralExpression (org.apache.phoenix.expression.LiteralExpression)1 ConstraintViolationException (org.apache.phoenix.schema.ConstraintViolationException)1 DelegateSQLException (org.apache.phoenix.schema.DelegateSQLException)1 PDataType (org.apache.phoenix.schema.types.PDataType)1