Java Code Examples for org.apache.calcite.plan.RelOptUtil#getColumnConstraints()

The following examples show how to use org.apache.calcite.plan.RelOptUtil#getColumnConstraints() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SqlValidatorImpl.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Validates updates against the constraint of a modifiable view.
 *
 * @param validatorTable A {@link SqlValidatorTable} that may wrap a
 *                       ModifiableViewTable
 * @param update         The UPDATE parse tree node
 * @param targetRowType  The target type
 */
private void checkConstraint(
	SqlValidatorTable validatorTable,
	SqlUpdate update,
	RelDataType targetRowType) {
	final ModifiableViewTable modifiableViewTable =
		validatorTable.unwrap(ModifiableViewTable.class);
	if (modifiableViewTable != null) {
		final Table table = modifiableViewTable.unwrap(Table.class);
		final RelDataType tableRowType = table.getRowType(typeFactory);

		final Map<Integer, RexNode> projectMap =
			RelOptUtil.getColumnConstraints(modifiableViewTable, targetRowType,
				typeFactory);
		final Map<String, Integer> nameToIndex =
			SqlValidatorUtil.mapNameToIndex(tableRowType.getFieldList());

		// Validate update values against the view constraint.
		final List<SqlNode> targets = update.getTargetColumnList().getList();
		final List<SqlNode> sources = update.getSourceExpressionList().getList();
		for (final Pair<SqlNode, SqlNode> column : Pair.zip(targets, sources)) {
			final String columnName = ((SqlIdentifier) column.left).getSimple();
			final Integer columnIndex = nameToIndex.get(columnName);
			if (projectMap.containsKey(columnIndex)) {
				final RexNode columnConstraint = projectMap.get(columnIndex);
				final ValidationError validationError =
					new ValidationError(column.right,
						RESOURCE.viewConstraintNotSatisfied(columnName,
							Util.last(validatorTable.getQualifiedName())));
				RelOptUtil.validateValueAgainstConstraint(column.right,
					columnConstraint, validationError);
			}
		}
	}
}
 
Example 2
Source File: SqlValidatorImpl.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Validates updates against the constraint of a modifiable view.
 *
 * @param validatorTable A {@link SqlValidatorTable} that may wrap a
 *                       ModifiableViewTable
 * @param update         The UPDATE parse tree node
 * @param targetRowType  The target type
 */
private void checkConstraint(
	SqlValidatorTable validatorTable,
	SqlUpdate update,
	RelDataType targetRowType) {
	final ModifiableViewTable modifiableViewTable =
		validatorTable.unwrap(ModifiableViewTable.class);
	if (modifiableViewTable != null) {
		final Table table = modifiableViewTable.unwrap(Table.class);
		final RelDataType tableRowType = table.getRowType(typeFactory);

		final Map<Integer, RexNode> projectMap =
			RelOptUtil.getColumnConstraints(modifiableViewTable, targetRowType,
				typeFactory);
		final Map<String, Integer> nameToIndex =
			SqlValidatorUtil.mapNameToIndex(tableRowType.getFieldList());

		// Validate update values against the view constraint.
		final List<SqlNode> targets = update.getTargetColumnList().getList();
		final List<SqlNode> sources = update.getSourceExpressionList().getList();
		for (final Pair<SqlNode, SqlNode> column : Pair.zip(targets, sources)) {
			final String columnName = ((SqlIdentifier) column.left).getSimple();
			final Integer columnIndex = nameToIndex.get(columnName);
			if (projectMap.containsKey(columnIndex)) {
				final RexNode columnConstraint = projectMap.get(columnIndex);
				final ValidationError validationError =
					new ValidationError(column.right,
						RESOURCE.viewConstraintNotSatisfied(columnName,
							Util.last(validatorTable.getQualifiedName())));
				RelOptUtil.validateValueAgainstConstraint(column.right,
					columnConstraint, validationError);
			}
		}
	}
}
 
Example 3
Source File: SqlValidatorImpl.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
/**
 * Validates insert values against the constraint of a modifiable view.
 *
 * @param validatorTable Table that may wrap a ModifiableViewTable
 * @param source        The values being inserted
 * @param targetRowType The target type for the view
 */
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);
			}
		}
	}
}
 
Example 4
Source File: SqlValidatorImpl.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Validates insert values against the constraint of a modifiable view.
 *
 * @param validatorTable Table that may wrap a ModifiableViewTable
 * @param source        The values being inserted
 * @param targetRowType The target type for the view
 */
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);
			}
		}
	}
}