private void checkConstraint ( SqlValidatorTable validatorTable , SqlNode source , RelDataType targetRowType ) { final ModifiableViewTable modifiableViewTable = validatorTable . unwrap ( ModifiableViewTable . class ) ; if ( modifiableViewTable != null && source instanceof SqlCall ) { final Table table = modifiableViewTable . unwrap ( Table . class ) ; final RelDataType tableRowType = table . getRowType ( typeFactory ) ; final List < RelDataTypeField > tableFields = tableRowType . getFieldList ( ) ; // Get the mapping from column indexes of the underlying table // to the target columns and view constraints. final Map < Integer , RelDataTypeField > tableIndexToTargetField = SqlValidatorUtil . getIndexToFieldMap ( tableFields , targetRowType ) ; final Map < Integer , RexNode > projectMap = RelOptUtil . getColumnConstraints ( modifiableViewTable , targetRowType , typeFactory ) ; // Determine columns (indexed to the underlying table) that need // to be validated against the view constraint. final ImmutableBitSet targetColumns = ImmutableBitSet . of ( tableIndexToTargetField . keySet ( ) ) ; final ImmutableBitSet constrainedColumns = ImmutableBitSet . of ( projectMap . keySet ( ) ) ; final ImmutableBitSet constrainedTargetColumns = targetColumns . intersect ( constrainedColumns ) ; // Validate insert values against the view constraint. final List < SqlNode > values = ( ( SqlCall ) source ) . getOperandList ( ) ; for ( final int colIndex : constrainedTargetColumns . asList ( ) ) { final String colName = tableFields . get ( colIndex ) . getName ( ) ; final RelDataTypeField targetField = tableIndexToTargetField . get ( colIndex ) ; for ( SqlNode row : values ) { final SqlCall call = ( SqlCall ) row ; final SqlNode sourceValue = call . operand ( targetField . getIndex ( ) ) ; final ValidationError validationError = new ValidationError ( sourceValue , RESOURCE . viewConstraintNotSatisfied ( colName , Util . last ( validatorTable . getQualifiedName ( ) ) ) ) ; RelOptUtil . validateValueAgainstConstraint ( sourceValue , projectMap . get ( colIndex ) , validationError ) ; } } } }
Validates insert values against the constraint of a modifiable view .