use of org.apache.calcite.rex.RexToSqlNodeConverter 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();
}
}
Aggregations