Search in sources :

Example 1 with RexSqlStandardConvertletTable

use of org.apache.calcite.rex.RexSqlStandardConvertletTable in project calcite by apache.

the class RelOptUtil method validateValueAgainstConstraint.

/**
 * Ensures that a source value does not violate the constraint of the target
 * column.
 *
 * @param sourceValue      The insert value being validated
 * @param targetConstraint The constraint applied to sourceValue for validation
 * @param errorSupplier    The function to apply when validation fails
 */
public static void validateValueAgainstConstraint(SqlNode sourceValue, RexNode targetConstraint, Supplier<CalciteContextException> errorSupplier) {
    if (!(sourceValue instanceof SqlLiteral)) {
        // We cannot guarantee that the value satisfies the constraint.
        throw errorSupplier.get();
    }
    final SqlLiteral insertValue = (SqlLiteral) sourceValue;
    final RexLiteral columnConstraint = (RexLiteral) targetConstraint;
    final RexSqlStandardConvertletTable convertletTable = new RexSqlStandardConvertletTable();
    final RexToSqlNodeConverter sqlNodeToRexConverter = new RexToSqlNodeConverterImpl(convertletTable);
    final SqlLiteral constraintValue = (SqlLiteral) sqlNodeToRexConverter.convertLiteral(columnConstraint);
    if (!insertValue.equals(constraintValue)) {
        // The value does not satisfy the constraint.
        throw errorSupplier.get();
    }
}
Also used : RexLiteral(org.apache.calcite.rex.RexLiteral) RexToSqlNodeConverterImpl(org.apache.calcite.rex.RexToSqlNodeConverterImpl) RexSqlStandardConvertletTable(org.apache.calcite.rex.RexSqlStandardConvertletTable) SqlLiteral(org.apache.calcite.sql.SqlLiteral) RexToSqlNodeConverter(org.apache.calcite.rex.RexToSqlNodeConverter)

Aggregations

RexLiteral (org.apache.calcite.rex.RexLiteral)1 RexSqlStandardConvertletTable (org.apache.calcite.rex.RexSqlStandardConvertletTable)1 RexToSqlNodeConverter (org.apache.calcite.rex.RexToSqlNodeConverter)1 RexToSqlNodeConverterImpl (org.apache.calcite.rex.RexToSqlNodeConverterImpl)1 SqlLiteral (org.apache.calcite.sql.SqlLiteral)1